golang中的类和接口的使用

时间:2025-02-24 09:51:44

类使用:实现一个people中有一个sayhi的方法调用功能,代码如下:

type People struct {
	//..
}

func (p *People) SayHi() {
	("************************* say hi !!")
}

func (this *LoginController) Get() {
	p := new(People)
	()

	 = ""
}

 

接口使用:实现上面功能,代码如下:

type People struct {
	//..
}

func (p *People) SayHi() {
	("************************* say hi !!")
}

type IPeople interface {
	SayHi()
}

func (this *LoginController) Get() {
	var p IPeople = new(People)
	()

	 = ""
}