💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 结构体的定义 ### 结构体是将零个或多个任意类型的命令变量组合在一起的聚合数据类型,每个变量都叫做结构体的成员 ### demo ``` package main import "fmt" type student struct { name string sex string age int scorc int } func studentStyle() { var stu student stu.name = "zhaoqi" stu.sex = "fale" stu.age = 20 stu.scorc = 100 fmt.Println(stu.name) } func main() { studentStyle() } ``` ## 关于go中的struct 1、用于定义复杂的数据结构 2、struct里面可以包含多个字段(属性),字段可以是任意类型 3、struct类型可以定义方法 4、struct类型可以嵌套 5、Go语言没有class类型,只有struct类型、 ## 结构体的比较 两个结构体将可以使用==或!=运算符进行比较。相等比较运算符将比较两个机构体的每个成员 ``` type Point struct{ x int y int } func main(){ p1 := Point{1,2} p2 :=Point{2,3} p3 := Point{1,2} fmt.Println(p1==p2) fmt.Println(p1==p3) } ``` ## 匿名字段 ``` type student struct { name string sex string age int int } func studentStyle() { var stu student stu.name = "zhaoqi" stu.sex = "fale" stu.age = 20 stu.int = 999 fmt.Println(stu.int) } func main() { studentStyle() } ``` ## 匿名字段的用处可能更多就是另外一个功能(其他语言叫继承) ``` package main import ( "fmt" ) type People struct{ Name string Age int } type Student struct{ People Score int } func main(){ var s Student /* s.People.Name = "tome" s.People.Age = 23 */ //上面注释的用法可以简写为下面的方法 s.Name = "tom" s.Age = 23 s.Score = 100 fmt.Printf("%+v\n",s) } ``` ### 关于结构体做参数 #### 不用指针就是值传递,加上指针就可以改变源地址,注意用了指针但是依旧用.进行调用