# 結構體的定義
### 結構體是將零個或多個任意類型的命令變量組合在一起的聚合數據類型,每個變量都叫做結構體的成員
### 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)
}
```
### 關于結構體做參數
#### 不用指針就是值傳遞,加上指針就可以改變源地址,注意用了指針但是依舊用.進行調用