一、struct聲明:golang
type 標識符 struct { Name string Age int Score int }
二、struct 中字段訪問:和其餘語言同樣,使用點
例子json
type Student struct { Name string Age int Score int } func main(){ var stu Student stu.Name = "lilei" stu.Age = 22 stu.Score = 20 fmt.Printf("name=%s age=%d score=%d",stu.Name,stu.Age,stu.Score) }
三、struct定義的三種形式:
a: var stu Student
b: var stu Student = new(Student)
c: var stu Student = &Student{}
(1)其中b和c返回的都是指向結構體的指針,訪問形式以下
stu.Name、stu.Age和stu.Score 或者(stu).Name、(stu).Age、(*stu).Scroemarkdown
type Rect1 struct { Min, Max point}
type Rect2 struct { Min, Max *point} 數據結構
type School struct { Name School Next *School }
每一個節點包含下一個節點的地址,這樣把全部的節點串起來,一般把鏈表的第一個節點叫作鏈表頭ide
type School struct { Name string Next *School Prev *School }
若是有兩個指針分別指向前一個節點和後一個節點叫作雙鏈表。函數
type School struct {
Name string
Left School
Right School
}
若是每一個節點有兩個指針分別用來指向左子樹和右子樹,咱們把這樣的結構叫作二叉樹佈局
package model type student struct { Name string Age int } func NewStudent(name string,age int) *student { return &student{ Name:name, Age:age, } }
package main S := new(student) S := model.NewStudent("tom",20)
type student struct { Name string `json="name"` Age string `json="age"` }
type Car struct { Name string Age int } type Train struct { Car Start time.Time int }
type Car struct { Name string Age int } type Train struct { Car Start time.Time Age int } type A struct { a int } type B struct { a int b int } type C struct { A B }