package main import "fmt" func main() { type student struct { id int name string sex byte //注意这里代表字符符,在初始化的时候这个值要用单引号括起来 age int addr string } //对结构体进行初始化,有两种,第一种,顺序初始化,不要忘记&取地址 var p1 *student = &student{1, "Baylor", 'm', 18, "NJ"} fmt.Println("s1 = ", *p1) fmt.Printf("p1 type is %T\n", p1) //第二种,指定成员初始化,如果没有被初始化的成员,则赋值为0 s2 := &student{name: "BaylorQu", addr: "NJ"} fmt.Println("s2 = ", *s2) }
执行结果
s1 = {1 Baylor 109 18 NJ} p1 type is *main.student //表示main里的结构体 s2 = {0 BaylorQu 0 0 NJ}