Understanding Interfaces in Go

Follow the latest of Whizdumb’s journey here.

In a world full of Object Oriented Languages, we have all worked with Interfaces, they are simple and elegant.

Go borrows the good things from the world of Object Oriented Programming and interfaces are one of the best things there. Lets see how we work with interfaces in Go,

type NamePrinter interface {
    printName()
}

Here we defined an interface called NamePrinter, they look similar to struct but rather than containing fields, interfaces contain methods, an interface can have more than one method.

In the previous post We had defined a struct Person which had a printName() method as shown

package main

import "fmt"

type Person struct {
  FirstName, MiddleName, LastName string 
}
func (p *Person) printName() {
    fmt.Println("The person name is",p.FirstName,p.MiddleName, p.LastName)
} 
func main() {
	p := Person{FirstName:"dumb", LastName:"whiz"}
	p.printName()
}

Now notice that our Interface NamePrinter also has the printName() method, this means that Person struct “implements” NamePrinter Interface. We do not need to explicitly mention that Person “implements” NamePrinter. How can we be sure that NamePrinter is indeed “implemented”?

Lets look at some code here

package main
import "fmt"

type NamePrinter interface {
     printName()
}

type Person struct {
  FirstName, MiddleName, LastName string 
}
func (p *Person) printName() {
    fmt.Println("The person name is",p.FirstName,p.MiddleName, p.LastName)
} 
func main() {
	p := Person{FirstName:"dumb", LastName:"whiz"}
	p.printName()
	checkInterfaceType(&p)
}

func checkInterfaceType(namePrinter NamePrinter){
   NamePrinter(namePrinter).printName()
   fmt.Println("interface type has been checked")
}

So, you have gone through the code. Go back and give it a look again. Seriously. Lets track our steps again the third time.

In the starting we declared our package and imported the fmt package, then we declared a new interface called NamePrinter.

Next we define a struct called Person with name fields in it. Next we define a method called printName() which has been “binded” to Person struct. Notice that the method name and signature is same as that defined in interface NamePrinter.

We start our main function and create a Person type variable with some initial values. In the next line we invoke the method printName() on a variable of Person type.

Fairly good up to this point.

In the next line we call checkInterfaceType function and pass in the “reference to p” or “address of p” (&p) variable.

In the next line checkInterfaceType() method is defined which accept a parameter of type NamePrinter. So that means the “reference of p” (&p) was of type NamePrinter, though we never really explicitly defined it anywhere. Inside the checkInterfaceType() function, we call printName() method on a variable referenced by NamePrinter.

Here is the output of the above code

The person name is dumb  whiz
The person name is dumb  whiz
interface type has been checked

The person type can implement multiple interfaces this way.

Go back and read this post again! Seriously!

~~ Whizdumb ~~

About these ads

One thought on “Understanding Interfaces in Go

  1. tilo says:

    Thanks, great article. You don’t need to apply type conversion inside `checkInterfaceType`. Since the argument is of type `NamePrinter` you can call it’s method directly (namePrinter.printName()).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s