英文:
Golang gob: type not registered for interface:
问题
所以我有一个类型String
,它是string
的别名定义:
type String string
然后我对它应用以下方法:
func (s String) String() string {
str := "'" + s + "'"
return string(str)
}
然后我尝试通过rpc发送一个包含String
的结构体,并得到以下错误:
gob: type not registered for interface: dbUtils.String
我没有定义任何同名的接口,为什么gob认为这是一个接口?
我用类似的类型遇到了同样的错误,但通过gob.Register(otherType{})
解决了它。
这对于String
不起作用,可能是因为string
不是一个接口。我对Go有点陌生,请解释一下发生了什么。
英文:
So I have a type String
that is an alias of string
defined:
type String string
I then apply the following method to it:
func (s String) String() string {
str := "'" + s + "'"
return string(str)
}
I then try to send a struct containing a String
over rpc and get the following error:
gob: type not registered for interface: dbUtils.String
I do not define any interfaces with the same name, so why does gob think this is an interface?
I got the same error with a similar type but solved it with gob.Register(otherType{})
.
This does not work with String
, presumably because string
is not an interface. I am somewhat new to go so please explain what is happening.
答案1
得分: 2
它应该适用于String,但请确保您传递的是String()对象的实例,而不是标识符String。所以使用gob.Register(String(""))
。
英文:
It should work with String, but make sure you're passing an instance of a String() object, not the identifier String. So use gob.Register(String(""))
答案2
得分: 2
我认为这可能是你试图实现的解决方案:
http://play.golang.org/p/9OrSKfcCAQ
请注意,你必须明确声明变量为 'String' 类型。如果你使用 ":=" 赋值,它会假设它是一个字符串并忽略函数。
英文:
I think this may be a solution to what you're trying to accomplish:
http://play.golang.org/p/9OrSKfcCAQ
Note that you have to explicitly declare the variable as a 'String'. If you use the ":=" assignment will assume it is a string and ignore the function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论