Go之路(十六):结构体
结构体
结构体也叫struct,与其他语言中class类似


除了直接var stu Student 之外 其他方式返回的都是指针
但是go已经做了优化,也可以直接stu.属性直接访问,正常来说应该加星号的
例子:
package main
import(
"fmt"
)
type Studnet struct{
Name string
Age int
socre float32
}
func main() {
var stu1 Studnet
stu1.Name = "小明"
stu1.Age = 12
stu1.socre = 99.5
fmt.Println(stu1)
fmt.Print(stu1.Name, stu1.Age, stu1.socre)
stu2 := new(Studnet)
stu2.Name = "小红"
stu2.Age = 13
stu2.socre = 89.5
fmt.Println(stu2)
fmt.Print(stu2.Name, stu2.Age, stu2.socre)
stu3 := &Studnet{
Name :"小绿",
Age :13,
socre :100,
}
fmt.Print(stu3.Name, stu3.Age, stu3.socre)
}
例子2链表(尾部插入法):
package main
import(
"fmt"
"math/rand"
)
type Student struct{
Name string
Age int
socre float32
Next *Student
}
func inserTail(p *Student){
var tail *Student = p
for i := 0; i<10; i++{
var stu *Student=new(Student)
stu.Name = fmt.Sprintf("stu%d",i)
stu.Age = rand.Intn(100)
stu.socre = rand.Float32()*100
tail.Next = stu
tail = stu
}
}
func trans(head *Student){
for head != nil{
fmt.Println((*head))
head = (*head).Next
}
}
func main() {
var head Student = Student{
Name:"tom",
Age:12,
socre:100,
}
inserTail(&head)
trans(&head)
}
头部插入法:
package main
import(
"fmt"
"math/rand"
)
type Student struct{
Name string
Age int
socre float32
Next *Student
}
func inserTail(p *Student){
var tail *Student = p
for i := 0; i<10; i++{
var stu *Student=new(Student)
stu.Name = fmt.Sprintf("stu%d",i)
stu.Age = rand.Intn(100)
stu.socre = rand.Float32()*100
tail.Next = stu
tail = stu
}
}
func trans(head *Student){
for head != nil{
fmt.Println((*head))
head = (*head).Next
}
}
func insertHead(head *Student) *Student{
for i := 0; i<10; i++{
var stu *Student=new(Student)
stu.Name = fmt.Sprintf("stu%d",i)
stu.Age = rand.Intn(100)
stu.socre = rand.Float32()*100
stu.Next = head
head = stu
}
return head
}
func main() {
var head Student = Student{
Name:"tom",
Age:12,
socre:100,
}
inserTail(&head)
// new_head := insertHead(&head)
trans(&head)
}
解释一下为什么要返回新指针,因为传入函数时候会复制一份指针,但是函数里边并没有对之前的数据进行操作。所以原值不变
结构体的另外一些方法
1.json中的tag功能
就是如果有tag字段的话,json打包的时候名称就会引用tag中所设置的
package main
import(
"fmt"
"encoding/json"
)
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
var stu1 = Student{
Name:"tom",
Age:12,
}
data,err := json.Marshal(stu1)
if err != nil{
fmt.Print("出错了")
}
fmt.Println(string(data))
}
结构体的方法
package main
import(
"fmt"
)
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
}
func (self *Student)init(name string, age int){
self.Name = name
self.Age = age
}
func main() {
var stu1 Student
stu1.init("tom",12)
fmt.Println(stu1)
}
结构体的继承是通过匿名字段来实现的
可以通过stu.结构体名.属性直接访问,如果有同名的,优先访问自己的
package main
import(
"fmt"
)
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
Father
Mother
}
type Father struct{
Name string
Age int
}
type Mother struct{
Name string
Age int
}
func (self *Student)init(name string, age int){
self.Name = name
self.Age = age
self.Father.Name = "小明"
self.Mother.Name = "小红"
}
func main() {
var stu1 Student
stu1.init("tom",12)
fmt.Println(stu1)
}

浙公网安备 33010602011771号