4.数据交换之结构体(beego)

4.数据交换之结构体(beego)

  • 结构体定义

type Person struct {
    Id int
    Name string
    Age  int
}
  • 初始化

// 第一种
var s Student = Student{name:"cxm",Age:18}


// 第二种
var s1 Student
s1.name = "cxm"
fmt.Println(s1)

// 第三种
s2 := Student{name:"cxm"}
  • 定义匿名字段(继承)

type Person struct {
    Id int
    Name string
}

type Student struct {
    Person
    Name string
}


s1.id = 3
  • 结构体方法定义

func (s Student) eat()  {
    fmt.Println("吃")
}
  • 方法继承

type Person struct {
    id int
}

func (p Person)hello()  {
    fmt.Println("hello")
}


type Student struct {
    Person
    name string
}

func (s Student) eat()  {
    fmt.Println("吃")
}


var s3 Student

s3.eat()
s3.hello()
  • 方法重写

type Person struct {
    id int
}

func (p Person)hello()  {
    fmt.Println("hello")
}


type Student struct {
    Person
    name string
}

func (s Student) eat()  {
    fmt.Println("吃")
}

func (s Student) hello()  {
    fmt.Println("hi")

}


var s3 Student

s3.eat()
s3.hello()

猜你喜欢

转载自blog.csdn.net/weixin_44908159/article/details/107582344