英文:
Interface method with multiple return types
问题
我正在努力处理接口的问题。考虑以下代码:
type Generatorer interface {
getValue() // 在这里应该放什么类型?
}
type StringGenerator struct {
length int
}
type IntGenerator struct {
min int
max int
}
func (g StringGenerator) getValue() string {
return "randomString"
}
func (g IntGenerator) getValue() int {
return 1
}
我希望getValue()
函数根据它是从StringGenerator
还是IntGenerator
调用而返回一个**string
或int
**。
当我尝试编译时,出现以下错误:
cannot use s (type *StringGenerator) as type Generatorer in array or
slice literal:
*StringGenerator does not implement Generatorer (wrong type for getValue method)have getValue() string
want getValue()
我该如何解决这个问题?
英文:
I'm struggling with interfaces. Consider this:
type Generatorer interface {
getValue() // which type should I put here ?
}
type StringGenerator struct {
length int
}
type IntGenerator struct {
min int
max int
}
func (g StringGenerator) getValue() string {
return "randomString"
}
func (g IntGenerator) getValue() int {
return 1
}
I want the getValue()
function to return a string
or an int
, depending on if it's called from StringGenerator
or IntGenerator
When I try to compile this, I get following error:
> cannot use s (type *StringGenerator) as type Generatorer in array or
> slice literal:
> *StringGenerator does not implement Generatorer (wrong type for getValue method)
>
> have getValue() string
> want getValue()
How can I achieve this?
答案1
得分: 6
你可以通过以下方式实现:
type Generatorer interface {
getValue() interface{}
}
type StringGenerator struct {
length int
}
type IntGenerator struct {
min int
max int
}
func (g StringGenerator) getValue() interface{} {
return "randomString"
}
func (g IntGenerator) getValue() interface{} {
return 1
}
空接口允许接收任何值。这使得代码可以更加通用,但也阻止了你使用Go强大的类型系统。
在你的示例中,如果你使用getValue
函数,你将得到一个类型为interface{}
的变量,如果你想使用它,你需要知道它实际上是一个字符串还是一个整数:你将需要大量使用reflect
,这会使你的代码变慢。
作为一个来自Python的开发者,我习惯于编写非常通用的代码。但是在学习Go时,我不得不改变这种思维方式。
关于你具体的情况,我无法给出具体建议,因为我不知道StringGenerator
和IntGenerator
被用于什么目的。
英文:
You could achieve it in this way:
type Generatorer interface {
getValue() interface{}
}
type StringGenerator struct {
length int
}
type IntGenerator struct {
min int
max int
}
func (g StringGenerator) getValue() interface{} {
return "randomString"
}
func (g IntGenerator) getValue() interface{} {
return 1
}
The empty interface allows every value. This allows for generic code but basically stops you from using the very powerful type system of Go.
In your example if you use the getValue
function, you will get a variable of type interface{}
and if you want to work with it, you need to know if it actually is a string or an int: you will need a lot of reflect
making your code slow.
Coming from Python I was used to code very generic. When learning Go I had to stop thinking that way.
What that means in your specific case I can't say because I don't know what StringGenerator
and IntGenerator
are being used for.
答案2
得分: 0
你无法按照你想要的方式实现这个。不过,如果你想要在不同的实现中返回不同的类型,你可以将函数声明为:
type Generatorer interface {
getValue() interface{}
}
英文:
You can't achieve this the way you want to. You can, however, declare the function as
type Generatorer interface {
getValue() interface{}
}
if you want it to return different types in different implementations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论