I come from java and currently try to learn go. I'm struggeling with interface
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 folowing 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 ?