This question already has an answer here:
I am new in Go and started learning about polymorphism.
I know how to do it when multiple objects need to use the same function.
But I have a new problem, I don't know what to do in a case I have the same variable on different objects.
In the following example I have two different objects: struct1
and struct2
. Both of them have the same variable name
.
I can run over them and check which is which and work accordingly (you can test it here):
package main
import (
"fmt"
)
type struct1 struct {
name string
}
type struct2 struct {
name string
}
func main(){
structMap := make(map[string]interface{})
s1 := struct1{name:"struct1_name"}
s2 := struct2{name:"struct2_name"}
structMap["struct1"] = s1
structMap["struct2"] = s2
for key, _ := range structMap {
switch key {
case "struct1":
generic := structMap[key].(struct1)
fmt.Println(generic.name)
case "struct2":
generic := structMap[key].(struct2)
fmt.Println(generic.name)
}
}
}
But if I had 20 objects ? I would need to do 20 checks?
So I wonder if it possible to do an interface with variables, something like:
type genericStruct interfcae {
name string
}
...
for key, _ := range structMap {
generic := structMap[key].(genericStruct)
fmt.Println(generic.name)
}
Of course this code doesn't work because I don't know how to do it, but I wanted to know about a way to do it.
EDIT:
I tried to use interface based on the example from:
https://gobyexample.com/interfaces
Thanks for Robbie Milejczak and the other guys who help me.
This the new working code:
package main
import (
"fmt"
)
type struct1 struct {
name string
}
type struct2 struct {
name string
}
type genericInterface interface {
GetName() string
}
func (r struct1 ) GetName() string{
return r.name
}
func (r struct2 ) GetName() string{
return r.name
}
func printName(g interface{}){
a := g.(genericInterface)
fmt.Println(a.GetName())
}
func main(){
structMap := make(map[string]interface{})
s1 := struct1{name:"struct1_name"}
s2 := struct2{name:"struct2_name"}
structMap["struct1"] = s1
structMap["struct2"] = s2
for key, _ := range structMap {
printName(structMap[key])
}
}
</div>