I've currently got an object from an external package that provides a fluent API, like:
thing := ext.GetThing()
thing.do().stuff()
I'd like to extend this to provide a new function cool
so I can use it like
thing.do().cool().stuff()
Unfortunately, since I can't have an external type as a receiver for cool
, then best I've got so far seems to be
cool(thing.do()).stuff()
which becomes very difficult to read as I combine my custom stuff with the built-in fluent.
Is there some way in Go to achieve the extended fluent style that I'm missing?
No, you can't because you "cannot define new methods on a non-local type", as the compiler will tell you if you try.
Let's say you want to do something silly like:
import "net/http"
func (c *http.Client) SayHi() {
println("Hi!")
}
This won't compile and you'll get the error message mentioned above as you can check here: https://play.golang.org/p/FxBMAfR_aN