type Student struct {
id int
name string
age int
}
// 顺序初始化,每个成员必须初始化
var s1 Student = Student{1, "mike", 18}
// 指定成员初始化,没有初始化的成员自动赋值
s1 := Student{name: "mike" }
var s Student
s.id = 1
s.name = "mike"
p2 := new(Student)
p2.id = 1
p2.name = "mike"
匿名字段
type Person struct {
name string
sex byte
age int
}
type Student struct {
Person
id int
addr string
}
func main() {
var s1 Student = Student{Person{"mike", 'm', 18}, 1, "bj"}
s2 := Student{Person{"mike", 'm', 18}, 1, "bj"}
s3 := Student{id: 1}
}