18.4 结构体

创建:

  1. type struct1 struct {
  2. field1 type1
  3. field2 type2
  4. }
  5. ms := new(struct1)

初始化:

  1. ms := &struct1{10, 15.5, "Chris"}

当结构体的命名以大写字母开头时,该结构体在包外可见。 通常情况下,为每个结构体定义一个构建函数,并推荐使用构建函数初始化结构体(参考例10.2):

  1. ms := Newstruct1{10, 15.5, "Chris"}
  2. func Newstruct1(n int, f float32, name string) *struct1 {
  3. return &struct1{n, f, name}
  4. }

链接