package main;import "fmt"//结构struct//定义Person结构type Person struct { name string; age int;};//结构里面还有一个匿名结构type Person2 struct { name string; age int; contact struct { phone string; addr string; }}//结构里的匿名字段type Person3 struct { string; int;}//嵌入结构,组合type Person4 struct { Person; phone string; addr string;}func main() { a := Person{}; a.name = "test"; a.age = 27; fmt.Println(a); //简单初始化方式 b := Person{ name: "test2", age: 24, }; fmt.Println(b); //调用函数A,这里是传递的值拷贝 A(b); fmt.Println(b); //调用函数B,这里传递的是地址 B(&b); fmt.Println(b); //对结构初始化的时候,一般直接用取地址符号 c := &Person{ name: "test3", age: 25, }; //在调用函数时,就不需要写取地址&符号 B(c); fmt.Println(c); //匿名结构 d := struct { name string; age int; }{ name: "test4", age: 22, }; fmt.Println(d); e := Person2{ name: "test5", age: 33, }; //匿名结构的初始化 e.contact.phone = "1388888888"; e.contact.addr = "地址"; fmt.Println(e); //注意字段的顺序 f := Person3{ "test6", 55, }; fmt.Println(f); //两个结构间的比较 g := f; fmt.Println(g == f); //嵌入结构的初始化 h := Person4{ Person: Person{name: "test7", age: 66}, phone: "139999999", addr: "地址2", }; fmt.Println(h); h.Person.name = "哈哈"; h.Person.age = 99; fmt.Println(h); //可以对name和age直接操作 h.name = "haohao"; h.age = 88; fmt.Println(h);}func A(p Person) { p.age = 33; fmt.Println(p);}func B(p *Person) { p.age = 33; fmt.Println(p);}