golang中结构体的初始化方法(new方法)
自定义一个结构体 type Rect struct { x, y float64 width, height float64 } 初始化方法: rect1 := new(Rect) rect2 := &Rect{} rect3 := &Rect{0, 0, 100, 200} rect4 := &Rect{width:100, height:200} 注意这几个变量全部为指向Rect结构的指针(指针变量),因为使用了new()函数和&操作符.而如果使用方法 a := Rect{} 则表示这个是一个Rect{}类型.两者是不一样的.参考代码: func main() { rect1 := &Rect{0, 0, 100, 200} rect1.x = 10 a := Rect{} a.x = ...阅读全文