您 可以 通过以下方式实现它:
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正在被使用。



