I wonder if is there solution for apply "polymorphism" in embedded functions when one pointing to other. For example I have following interface:
type Client interface {
Get(string) string
GetResource() string
}
and default implementation:
type ClientImpl struct {
}
func (c ClientImpl) Get(s string) string {
return fmt.Sprintf("Impl [%s]", s)
}
func (c ClientImpl) GetResource() string {
return c.Get("resource") # points to Get
}
And in other implementation (tests for example) I want to replace default Get function with other response but leave GetResource method untouched
type TestImpl struct {
ClientImpl
}
func (t TestImpl) Get(s string) string {
return fmt.Sprintf("Test [%s]", s)
}
Override function works when it's called directly but doesn't when called from embedded function. Look output produced from following test case:
c := ClientImpl{}
t := TestImpl{}
fmt.Println("WORKS:")
fmt.Println("Client Get:", c.Get("aaa"))
fmt.Println("Test Get:", t.Get("aaa"))
fmt.Println("DOSN'T WORK :(")
fmt.Println("Client GetRes:", c.GetResource())
fmt.Println("Test GetRes:", t.GetResource())
# WORKS:
# Client Get: Impl [aaa]
# Test Get: Test [aaa]
# DOSN'T WORK :(
# Client GetRes: Impl [resource]
# Test GetRes: Impl [resource]
How to make last print to output string Test [resource]
?
Go Playground example: https://play.golang.org/p/b-vM1_W3oB