Another way to create a variable is called with the new built-in functions. Expressions new (T) T will create a type of anonymous variable is initialized to zero value of type T, and then return to the variable address pointer return type is * T.
the p-: new new = (int) the p-//, * int type, int variables point to anonymous fmt.Println ( * P) // " 0 " * P = 2 // int anonymous variable is set 2 fmt.Println ( * P) // " 2 "
Creating variables and common way to create variable variable declaration statement is no different with the new, in addition to not need to declare a temporary variable name, but we can also be used in expressions new new (T) . In other words, new new similar function is a syntactic sugar, rather than a new foundation concept.
The following two newInt function has the same behavior:
func newInt() *int { return new(int) } func newInt() *int { was dummy int return &dummy }
Each call is a new function returns the address of a new variable, so the following two addresses are different:
p := new(int) q := new(int) fmt.Println(p == q) // "false"