8.结构体与方法 package main import ( "fmt" ) //1.结构体定义 type auther struct { a1 int a2 string a3 float32 } //递归结构体 type node struct { pr *node //前去节点 data float32 su *node //后驱节点 } //结构体转换 type number struct { f float32 } //别名 type nr number //结构体的参数传递 //形参 func Auther_test1(Auther auther){ Auther.a1=123 } //指针 func Auther_test2(Auther *auther){ Auther.a1=234 } //指针 func main() { at := new(auther) at.a1 = 12 at.a2 = "你" at.a3 = 12.3 fmt.Printf("int:%d\n", at.a1) //2.初始化结构体的值 ms := &auther{12, "王", 123.23} fmt.Printf(ms.a2) ma := auther{13, "li", 12.13} fmt.Printf("%d\n", ma.a1) //三种初始化方式 mb := auther{0, "a", 12.1} mc := auther{a1: 12, a2: "zh", a3: 12.12} md := auther{a3: 12.3} fmt.Println(mb.a1, mc.a1, md.a3) //转换 a1 := number{5.0} a2 := nr{6.0} var c = number(a2) fmt.Println(a1, a2, c) //结构体参数函数调用 var at1 auther //声明结构体,这里new出的不行,使用var声明 Auther_test1(at1) fmt.Println(at1.a1) //不是指针的不会影响 Auther_test2(&at1) fmt.Println(at1.a1) //是指针的会影响 }