Could anybody explain what the subtle difference between these two notations: (*T)(nil)/new(T)
and &T{}
.
type Struct struct {
Field int
}
func main() {
test1 := &Struct{}
test2 := new(Struct)
test3 := (*Struct)(nil)
fmt.Printf("%#v, %#v, %#v
", test1, test2, test3)
//&main.Struct{Field:0}, &main.Struct{Field:0}, (*main.Struct)(nil)
}
Seems like the only difference of this one (*T)(nil)
from other is that it returns nil pointer or no pointer, but still allocates memory for all fields of the Struct.