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 ~~

Advertisements

Introducing Structures and Methods

Follow the latest of Whizdumb’s journey here.

Like in all programming languages, Go also provides a way to create your own data types by combining two or more basic types.

To do that Go provide us with struct keyword. Lets try and create a new type “Person”. All we need to do is

type Person struct {} 

Lets add some fields to person

type Person struct {
  FirstName, MiddleName, LastName string 
}

Now lets create an instance for this Person

var p Person

This creates an instance of Person referred by p and initialises all fields in Person to empty strings. If we try to print values for person p, we will get empty strings. Following code demonstrates that.

package main
import "fmt"
type Person struct {
  FirstName, MiddleName, LastName string 
} 
func main() {
	var p Person
	fmt.Println("The person name is ", p.FirstName, p.MiddleName, p.LastName)
}

This gives output

The person name is    

Here also notice how we accessed the fields of Person using “.” (dot) operator.

We can also use the new function to create variable.

p := new(Person)

Thing to note here is that it will return a pointer to the Person, so p here in essence is pointer to the Person struct. p is of type *Person

Now lets try to create a new variable and set some values for name of the person

 p := Person{FirstName:"dumb", LastName:"whiz"}

If we try to print the names now, we will get the person name. Here is how the program looks like with output

package main

import "fmt"

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

output :

The person name is dumb  whiz

We can create a function which accepts person as a parameter and prints the name. But, what if we wanted to invoke a function on Person type, like p.printName() where p is a variable of Person type.

Go provides a way to “bind” functions to a particular type and such functions are knowns as methods. Lets see how we do it

func (p *Person) printName() {
    fmt.Println("The person name is",p.FirstName,p.MiddleName, p.LastName)
} 

In the above code we define a method printName() which accepts no parameter but can be invoked by a variable of type Person.

Here is the complete code sample

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()
}

Neat!

~~ Whizdumb ~~