type check go interface implementations on compile time 👮

· sepi's blog

#go #programming #interface

interfaces in go are a bit different from other languages namely java and c#, in go you do not explicitly define a concrete implementation for an interface, like what you do in say java. in fact interfaces are impelmented implicitly, in other words an struct implments an interface once in contains all the method defined in an interface.

for example lets say we have the following interfaces:

1package x
2
3type Fooer interface {
4  Foox(in string) int
5  Fooy(in int) string
6}
1package y
2
3type Bazer interface {
4  Baz(in string) string
5}

now these interfaces are implemented in the following struct:

1package sample
2
3type Bar {}
4
5func (b Bar) Foox(in string) int { /*some code goes here*/ }
6
7func (b Bar) Fooy(in int) string { /*some code goes here*/ }
8
9func (b Bar) Baz(in string) string { /*some code goes here*/ }

notice that we did not mentioned the name of the above interfaces, heck we did not even import those modules, yet golang knows that Bar is compatible with both Fooer and Bazer. this is possible because go compiler only cares about the contracts that each interface defines, and the contract is simple the methods definiton.

this makes the code very cleaner and less hairy when you are in a large code base, however anything that is used implicitly can change behinde the scene and bit you when you do not expect. one way to quickly cacth errors when you deviate from the interfaces contracts is to write something like this at the top of the file that implements an interface or interfaces:

 1package sample
 2
 3import (
 4  "x" //package that defines Fooer
 5  "y" //package that defines Bazer
 6)
 7
 8var _ x.Fooer = Bar{} // makes sure that Bar implements Fooer
 9var _ y.Bazer = Bar{} // makes sure that Bar implements Bazer
10
11type Bar {}
12
13// the same code as before

now if you change anything in your implementation code or if something changes on the interfaces side you get a clean compiler error complaining that you are deviating from the interface.

nice?

meme