您好,登錄后才能下訂單哦!
1、struct聲明:
type 標(biāo)識(shí)符 struct {
Name string
Age int
Score int
}
2、struct 中字段訪問:和其他語言一樣,使用點(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)體的指針,訪問形式如下
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)串起來,通常把鏈表的第一個(gè)節(jié)點(diǎn)叫做鏈表頭
type School struct {
Name string
Next *School
Prev *School
}
如果有兩個(gè)指針分別指向前一個(gè)節(jié)點(diǎn)和后一個(gè)節(jié)點(diǎn)叫做雙鏈表。
type School struct {
Name string
Left School
Right School
}
如果每個(gè)節(jié)點(diǎn)有兩個(gè)指針分別用來指向左子樹和右子樹,我們把這樣的結(jié)構(gòu)叫做二叉樹
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
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。