|
|
|
|
|
by Goopplesoft
4210 days ago
|
|
Go's interfaces are great, but one of my annoyances is that you can't define an interface with values (methods only). Which makes little sense considering it works just fine with getters and setters. i.e: type Blah interface { Var string }
wont work, but type Blah interface { GetVar() string }
does. I'm sure theres a reason for this. Can someone shed some light? |
|
In your example, GetVar() can be implemented in a number of ways. Returning Var field of Blah struct is only one such way (and if there was only one implementation, there would be no need for an interface).
In that light, adding variables as part of interface doesn't make sense. Variables are fixing the thing that interface is meant to make flexible.
In Go, you can achieve re-using of a bunch of variables (and their methods) by embedding - put the variables you want to re-use into a separate struct and embed that struct in other structs. See https://golang.org/doc/effective_go.html#embedding