结构体定义过程如下:
type 类型名 struct {
字段1 字段1类型
字段2 字段2类型
…
}
结构体定义与调用的例子
type Student struct {
Name string
Age int
score int
}
func main() {
var stu = Student{
Name: "fxx",
Age: 13,
score: 99,
}
fmt.Println(stu)
var stu1 *Student = &Student{
Name: "dage",
Age: 9999,
score: 0,
}
fmt.Println(stu1)
fmt.Printf("Name:%p \n", &stu1.Name)
fmt.Printf("Age:%p \n", &stu1.Age)
fmt.Printf("score:%p \n", &stu1.score)
var stu2 Student
stu2.score = 1
stu2.Age = 2
stu2.Name = "didi"
fmt.Println(stu2)
}
利用结构体实现单链表的头部插入:
type student struct {
Name string
Age int
score int
next *student
}
func flowinsert(a *student) *student {
for i := 0; i < 10; i++ {
var stu student
stu.Name = fmt.Sprintf("stu%d", i)
stu.Age