接口方法具有多个返回类型

huangapple go评论74阅读模式
英文:

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调用而返回一个**stringint**。

当我尝试编译时,出现以下错误:

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时,我不得不改变这种思维方式。

关于你具体的情况,我无法给出具体建议,因为我不知道StringGeneratorIntGenerator被用于什么目的。

英文:

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.

huangapple
  • 本文由 发表于 2017年7月12日 19:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/45055953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定