Understanding Interfaces in Go

Whiz Dumb's Blog

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 {

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"}

Now notice that our Interface NamePrinter also has the printName() method, this means that Person struct “implements”…

View original post 316 more words