以下代码创建struct的可用实例Car。这与使用new(Car)有何不同?
Car
new(Car)
例:
type Car struct { make string } func Main() { var car Car; // how is this different than "car := new(Car)"? car.make = "Honda" }
一个定义了Car变量,另一个定义了指向Car的指针。
var car Car // defines variable car is a Car car2 := new(Car) // defines variable car2 is a *Car and assigns a Car to back it
car := new(Car)可以这样实现var car Car:
car := new(Car)
var car Car
var x Car car := &x