Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That really depends on what you're tying to do. Say you have a Datastore interface, which has some methods like "Create", "Update", "Delete", and a MySQL + an inmem implementation.

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



Wow, that seems like a big issue. Is this accepted as a bug and being worked on or yet another feature?


It's not a bug. Interfaces are reference types. In this case, the interface is implemented by a pointer type. The interface can be thought of as a pointer to a nil pointer, but the "interface pointer" itself is not nil.

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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: