In Go or Golang, declaring an interface is pretty simple and easy.
1 2 3 |
type Printer interface { Print(string) } |
We just defined an interface named Printer
that required an implementer to have a method named Print
which takes a string
parameter and returns nothing. Interfaces are implemented implicitly in Go. Any type that has the Print(string)
method implements the interface. There is no need to use any implements
keyword or anything of that sort.
1 2 3 4 5 6 |
type Terminal struct { } func (t Terminal) Print(message string) { fmt.Println(message) } |
In the above example, the Terminal
type implements the Printer
interface because it implements the methods required by the interface. Here’s a runnable, full code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package main import ( "fmt" ) type Printer interface { Print(string) } type Terminal struct { } func (t Terminal) Print(message string) { fmt.Println(message) } func main() { var printer Printer printer = Terminal{} printer.Print("Hello World!") } |
We declared our printer
variable to be of type Printer
which is the interface. Since the Terminal
type implements the Printer
interface, we can pass Terminal{}
to the printer
variable and later call the Print
method on it.
Interface and the Method sets
As you can understand, a method set is a set of methods on a type. The method set of an interface type (for example Printer
here) is it’s interface, that is the Print
method in this example. The method set of a type T
(for example Terminal
) contains the methods which can take a T
type receiver. In our above code, the Print
method takes the type Terminal
so it’s included in Terminal
‘s method set. The corresponding pointer type, *T
has a method set that includes all methods with a receiver type of *T
as well as the methods defined on the receiver type T
. So *Terminal
type contains any method that either takes Terminal
or Terminal
as a receiver type. So the Print
method is also in the method set for *Terminal
.
Method Set of T
includes all methods receiving just T
.
Method Set of *T
includes all methods receiving either T
or *T
.
So the method set of *T
includes the method set of T
anyway. But by now, you might be wondering why this is so important. It is very important to understand the method set of a type because whether it implements an interface or not depends on the method set. To understand things further, let’s take a quick look at the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package main import ( "fmt" ) type Printer interface { Print(string) } type Terminal struct { } func (t *Terminal) Print(message string) { fmt.Println(message) } func main() { var printer Printer printer = Terminal{} printer.Print("Hello World!") } |
If you try to run this code on the go playground or try to run/compile it on your machine, you shall get an error message like this:
1 2 |
.\main.go:20:10: cannot use Terminal literal (type Terminal) as type Printer in assignment: Terminal does not implement Printer (Print method has pointer receiver) |
Can you guess what’s happening? Well, our Print
method has a receiver type of *Terminal
however we are trying to assign the type Terminal
to printer
. The Print
method falls in the method set of *Terminal
and not Terminal
. So in this particular example, *Terminal
type actually implements the interface, not the base Terminal
type. We can just assign &Terminal
to printer
and it will work fine. Try the codes here – https://play.golang.org/p/MvyD0Ls8xb 🙂
Another interesting thing, since *Terminal
also includes the method set defined on Terminal
, this could would be valid just fine – https://play.golang.org/p/xDmNGBcwsM. This is why understanding the method set of a type is important to understand which interfaces it implements.
The Curious Case of Method Calls
We have seen how the method set of *T
includes methods receiving both T
and *T
but the method set of T
is confined to methods that only take T
and not *T
. Now you might be thinking – I have seen codes like the following snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package main import ( "fmt" ) type Printer interface { Print(string) } type Terminal struct { } func (t *Terminal) Print(message string) { fmt.Println(message) } func main() { var terminal Terminal terminal = Terminal{} terminal.Print("Hello!") } |
Here, the Print
method receives a *Terminal
type but how are we calling it on Terminal
type? From what we have seen before, Terminal
should not have the method set defined to take a *Terminal
receiver, how is this call being made?
Well, the code x.m()
works fine if the m
method takes the x
type as receiver. That is fine with us. But if the method m
is to take the type *x
and we try to call x.m()
– that shouldn’t work, right? The proper call should be (&x).m()
– no? Yes, correct. But Go provides us a shortcut here. If the method m
is defined to take a *x
type as receiver and base type x
is addressable, x.m()
works as a shortcut for (&x).m()
. Go provides us with that shortcut to keep things simpler. So whether you have a pointer or a value, it doesn’t matter, as long as the type is addressable, you can call the method set of *x
on x
using the very same syntax. However, please remember that this shortcut is not available while working with interfaces.
The Empty Interface
The type interface{}
has zero methods defined on it. And every type in Go implements zero or more methods. So their method set actually satisfies the emtpy interface aka interface{}
. So if a variable is of type interface{}
, we can pass any type to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package main import ( "fmt" ) func main() { var x interface{} x = 2 fmt.Println(x) x = "masnun" fmt.Println(x) } |
We want to store different types in the same slice? Map values can be of different types? Just use interface{}
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package main import "fmt" func main() { things := []interface{}{} things = append(things, "masnun") things = append(things, 42) fmt.Println(things) unKnownMap := map[string]interface{}{} unKnownMap["name"] = "masnun" unKnownMap["life"] = 42 fmt.Println(unKnownMap) } |
So, when we’re not sure of a type, or we need the type to flexible / dynamic, we can use interface{}
to store them.
Type Assertion
While we can store any type in a interface{}
type, not all types are the same. For example, you can not use the string functions on an integer type. Go would not accept if you blindly want to pass interface{}
in an operation where a very specific type is expected. Take a look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package main import ( "strings" ) func main() { unKnownMap := map[string]interface{}{} unKnownMap["name"] = "masnun" unKnownMap["life"] = 42 strings.ToUpper(unKnownMap["name"]) } |
Even though we have a string value stored against the name
key, Go actually stores it as a type interface{}
and thus it won’t allow us to use it like a string. Luckily, interface values do store the underlying value and type. So we can use type assertion to assert that the underlying value can behave like a certain type.
This works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package main import ( "strings" "fmt" ) func main() { unKnownMap := map[string]interface{}{} unKnownMap["name"] = "masnun" unKnownMap["life"] = 42 fmt.Println(strings.ToUpper(unKnownMap["name"].(string))) } |
The unKnownMap["name"].(string)
part – we’re doing the type assertion here. If the type assertion succeeds, we can use the value as a string. If it does not succeed, we will get a panic.
Getting Type of an interface{}
If you have an interface{}
and want to know what it holds underneath, you can use the %T
format in Printf
family of calls.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package main import ( "fmt" ) func main() { unKnownMap := map[string]interface{}{} unKnownMap["name"] = "masnun" unKnownMap["life"] = 42 fmt.Printf("%T \n", unKnownMap["name"]) fmt.Printf("%T \n", unKnownMap["life"]) } |
Type Switch
You can also use a switch statement with an interface{}
to deal with different possible types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package main import ( "fmt" ) func main() { unKnownMap := map[string]interface{}{} unKnownMap["name"] = "masnun" unKnownMap["life"] = 42 TypeSwitch(unKnownMap["name"]) TypeSwitch(unKnownMap["life"]) } func TypeSwitch(i interface{}) { switch i.(type) { case string: fmt.Println("String Value: ", i.(string)) case int: fmt.Println("Integer Value: ", i.(int)) } } |
The i.(type)
gets you the type of the variable. Please remember it only works with a switch statement.