Go is not a (completely) object-oriented language : it does not have classes, but does not have type inheritance ; but it supports a similar construct called attachment at both the struct level and the interface level, and it has methods .
Interfaces in Go are just fixed sets of methods. A type implicitly implements an interface if its set of methods is a superset of the interface (there is no declaration of intent).
Empty methods are great if you want to explicitly or explicitly state that your type implements an interface (because it is not explicitly specified). Official Go FAQ: How can I guarantee that my type satisfies the interface?
type Fooer interface { Foo() ImplementsFooer() }
If you want to distinguish between a type hierarchy (for example, you do not want the object to be both Movable and Immovable ), they must have different sets of methods (there must be at least one method in each of the sets of Movable and Immovable methods that is missing in others), because if the sets of methods contain the same methods, the implementation of one of them will automatically implement the other, so you can assign a Movable object to a variable of type Immovable .
Adding an empty method to an interface with the same name will give you this distinction, assuming that you will not add such methods to other types.
Reducing the number of empty methods
Personally, I have no problem with empty methods. However, there is a way to reduce them.
If you also create a struct implementation for each type in the hierarchy, and each implementation implements the struct implementation one level higher, the method set one level higher will automatically exit without any noise:
An object
Object and ObjectImpl implementation:
type Object interface { object() } type ObjectImpl struct {} func (o *ObjectImpl) object() {}
The property
Immovable and ImmovableImpl implementation:
type Immovable interface { Object immovable() } type ImmovableImpl struct { ObjectImpl
Note ImmovableImpl only adds the immovable() method, object() "inherited".
Building
Building implementation:
type Building struct { ImmovableImpl
Note Building does not add any new methods, but automatically it is an Immovable object.
The advantage of this technique increases significantly if the number of subtypes increases or if interface types have more than one token method (since all methods are inherited).