In the Go language, all uninitialized variables are initialized to zero values of this type. for example, if the zero value of the bool type is false and the zero value of the int type is 0, the zero value of the string type is a null string.
Customize a struct
| 1234 |
Type Rect struct {x, y float64 width, height float64} |
Initialization method:
| 1234 |
Rect1: = new (Rect) rect2: = & Rect {} rect3: = & Rect {100,200, 100} rect4 :=& Rect {width: 200, height} |
In the Go language, all uninitialized variables are initialized to zero values of this type. for example, if the zero value of the bool type is false and the zero value of the int type is 0, the zero value of the string type is a null string.
There is no constructor concept in the Go language. object creation is usually done by a global creation function. NewXXX is used as a command to represent "constructor ":
| 123 |
Func NewRect (x, y, width, ehgith float64) {return & Rect {x, y, width, height }} |
This is all natural. developers do not need to analyze how many things happen after new is used. in The Go language, everything that will happen can be seen directly.