[Go] New

New is alernate way to create a variable

new() function create a variable and returns a pointer to the variable

Variable is initialized to zero

package main

import "fmt"

func main() {
	ptr := new(int)

	fmt.Println(*ptr) // 0

	*ptr = 3

	fmt.Println(*ptr) // 3
}

 

posted @ 2022-09-07 15:06  Zhentiw  阅读(15)  评论(0编辑  收藏  举报