1 min readDec 8, 2017
Go totally has a method_exists() solution. It’s done with defining an interface and then doing a type assertion.
Here’s some sample code:
type HasColorMethod interface {
Color() string
}func PrintColor(thing interface{}) {
if c, ok := thing.(HasColorMethod); ok {
fmt.Println(c.Color())
} else {
fmt.Println("missing color method")
}
}
You can see/run an example here.