1、struct聲明:
創(chuàng)新互聯(lián)建站自2013年創(chuàng)立以來(lái),是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元溫宿做網(wǎng)站,已為上家服務(wù),為溫宿各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
type 標(biāo)識(shí)符 struct {
Name string
Age int
Score int
}
2、struct 中字段訪問(wèn):和其他語(yǔ)言一樣,使用點(diǎn)
例子
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)
}
3、struct定義的三種形式:
a: var stu Student
b: var stu Student = new(Student)
c: var stu Student = &Student{}
(1)其中b和c返回的都是指向結(jié)構(gòu)體的指針,訪問(wèn)形式如下
stu.Name、stu.Age和stu.Score 或者(stu).Name、(stu).Age、(*stu).Scroe
type Rect1 struct { Min, Max point}
type Rect2 struct { Min, Max *point}
type School struct {
Name School
Next *School
}
每個(gè)節(jié)點(diǎn)包含下一個(gè)節(jié)點(diǎn)的地址,這樣把所有的節(jié)點(diǎn)串起來(lái),通常把鏈表的第一個(gè)節(jié)點(diǎn)叫做鏈表頭
type School struct {
Name string
Next *School
Prev *School
}
如果有兩個(gè)指針?lè)謩e指向前一個(gè)節(jié)點(diǎn)和后一個(gè)節(jié)點(diǎn)叫做雙鏈表。
type School struct {
Name string
Left School
Right School
}
如果每個(gè)節(jié)點(diǎn)有兩個(gè)指針?lè)謩e用來(lái)指向左子樹(shù)和右子樹(shù),我們把這樣的結(jié)構(gòu)叫做二叉樹(shù)
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
}