GO方法集问题cannot use xxxxx as xxxxx value in variable declaration

Posted XY丶YX

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GO方法集问题cannot use xxxxx as xxxxx value in variable declaration相关的知识,希望对你有一定的参考价值。

注:
本文代码来自https://zhuanlan.zhihu.com/p/47616970
解释资料来自https://www.jianshu.com/p/d93656cdce0a

先看这一段代码

package main

import (
	"fmt"
)

type People interface {
	Speak(string) string
}

type Stduent struct{}

func (stu *Stduent) Speak(think string) (talk string) {
	if think == "eat" {
		talk = "You are a good boy"
	} else {
		talk = "hi"
	}
	return
}

func main() {
	var peo People = Stduent{}
	think := "eat"
	fmt.Println(peo.Speak(think))
}

这段代码是编译不通过的,原因如下
cannot use (Stduent literal) (value of type Stduent) as People value in variable declaration: missing method Speak (Speak has pointer receiver)
我就感觉奇怪,明明Student实现了Speak方法,为什么报错说没有实现呢?

这里就涉及到一个【方法集】的概念了,第一次接触的同学可以去百度学习一下。
其中,Student实现的Speak方法中,"(stu Student)"这个叫做接收器(receiver),如果变量是T,那么接收器类型为(t T)的才算变量T实现的方法;如果变量是T,那么接收器类型为(t T)和(t *T)的都算变量T实现的方法。写成表格的形式如下:

变量类型方法接收器类型
T(t T)
*T(t T)+(t *T)

看回我们的代码,peo声明的是Student{},也就是普通的T,所以只能接收(t T)的方法,而我们的Speak是(stu *Student)的,所以就有了上面的错误。
修改很简单,把peo声明为指针类型,或者把接收器的指针标识去掉

方法1:修改变量类型

package main

import (
	"fmt"
)

type People interface {
	Speak(string) string
}

type Stduent struct{}

func (stu *Stduent) Speak(think string) (talk string) {
	if think == "eat" {
		talk = "You are a good boy"
	} else {
		talk = "hi"
	}
	return
}

func main() {
	var peo People = &Stduent{}
	think := "eat"
	fmt.Println(peo.Speak(think))
}

方法2:修改接收器类型

package main

import (
	"fmt"
)

type People interface {
	Speak(string) string
}

type Stduent struct{}

func (stu Stduent) Speak(think string) (talk string) {
	if think == "eat" {
		talk = "You are a good boy"
	} else {
		talk = "hi"
	}
	return
}

func main() {
	var peo People = Stduent{}
	think := "eat"
	fmt.Println(peo.Speak(think))
}

方法3:
改成 peo := Stduent{} 即可

以上是关于GO方法集问题cannot use xxxxx as xxxxx value in variable declaration的主要内容,如果未能解决你的问题,请参考以下文章

GO方法集问题cannot use xxxxx as xxxxx value in variable declaration

GO方法集问题cannot use xxxxx as xxxxx value in variable declaration

Eclipse报The default workspace'xxxxx' is in use or cannot be created Pl

GO 智能合约cannot use transactionRecordId + strconv.Itoa(id) (type string) as type byte in append(示例代码(代

Cannot use this in a static context

python3 学习:在网站地图爬虫时的cannot use a string pattern on a bytes-like object 问题的解决方法