struct引用初始化
在初始化结构引用时,请使用&T{}
代替new(T)
,以使其与结构体初始化一致。
//bad usage
sval := T{Name: "foo"}
// inconsistent
sptr := new(T)
sptr.Name = "bar"
//good usage
sval := T{Name: "foo"}
sptr := &T{Name: "bar"}
Last updated
Was this helpful?
在初始化结构引用时,请使用&T{}
代替new(T)
,以使其与结构体初始化一致。
//bad usage
sval := T{Name: "foo"}
// inconsistent
sptr := new(T)
sptr.Name = "bar"
//good usage
sval := T{Name: "foo"}
sptr := &T{Name: "bar"}
Last updated
Was this helpful?