天天看点

Golang的struct,类的定义和使用

文章目录

    • 结构体定义
    • 类的定义

结构体定义

// 定义结构体
type Student struct {
	id int
	name string
	age int
}
           

类的定义

就是通过结构体绑定方法
func (this *Student) GetName() string {
	return this.name
}
func (this *Student) SetName(name string) {
	this.name = name
}
func (this *Student) Show() {
	fmt.Println("student = ", this)
}

func main() {
	var student = Student{1, "name1", 18)
	name := student.GetName()
	fmt.Println("student name = ", name)
	student.SetName("goodName")
	student.Show()
}
           

继续阅读