I recently heard a rule of thumb: "accept interfaces, return structs".
I haven't put it into practice, but it seems like this should give callers enough flexibility to pass in whatever they like.
I haven't put it into practice, but it seems like this should give callers enough flexibility to pass in whatever they like.
You can have `datastore.New() Datastore`, which returns one of the implementations, or you can have `datastore.NewMySQLStore, datastore.NewInMemStore()`. Now you need a different method for each implementation.
Also be careful with returning concrete type, you can run into this issue: https://play.golang.org/p/WHAW1X6elS
This is a tricky concept until you learn that interfaces are reference types.
Here's a simplified version of the previous example that illustrates this concept: https://play.golang.org/p/pODBgaHXq5