一、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 }