Golang : interface - when and where to use examples

Tags : golang interface unmarshal object-oriented

Question :

New comer to Golang will need to learn about interface eventually. So what exactly is interface? Where and when to use interface in Golang. Can show some examples?

Discussion :

What exactly is interface?

In Golang, an interface can be a type and also a set of methods. Interface ... according to the official documentation : "Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here. "

In simplest term, interface allows a developer to write program that deal with some uncertainty during run time and unknown data type during run time. Like allowing a function to accept whatever parameters

 func AFunction(v interface{}) {

Where and when to use interface?

When you have a function that will behave according to the input data. The input data will determine which method to use.

For example :

 package main

 import (

 type SayHelloIntFace interface {

 type Person struct{}

 // this method is tied to Person class
 func (person Person) SayHello() {

 type Dog struct{}

 // this method is tied to Dog class
 func (dog Dog) SayHello() {
  fmt.Printf("woof! woof!")

 func greeting(i SayHelloIntFace) {

 func main() {
  // instantiate objects
  person := Person{}
  dog := Dog{}

  var i SayHelloIntFace

  fmt.Println("\nPerson : ")
  i = person

  fmt.Println("\n\nDog : ")
  i = dog



The greeting() function will accept the input data type SayHelloIntFace(object) and because ... "Interfaces in Go provide a way to specify the behavior of an object"....this is why the program aforementioned will be able to automatically select which SayHello() method to use.

Another place where your program want to use interface is when your program can't determine the input data type during run time. For example, your program is expecting JSON data and you want to un-marshal the JSON data to map. You know that top-level keys are strings, but not the lower-level values data type during runtime. It could be strings, integer or rune. Interface type is perfectly suited for this kind of situation.

 package main

 import (

 func main() {
  // Given a possibly complex JSON object
  msg := "{\"assets\" : {\"old\" : 123}}"

  // We only know our top-level keys are strings - "assets"
  // but the lower-level value could integer, rune or string
  // therefore, we create a make use of interface{} type
  mp := make(map[string]interface{})

  // Decode JSON into our map
  err := json.Unmarshal([]byte(msg), &mp)
  if err != nil {

  // See what the map has now
  fmt.Printf("mp is now: %+v\n", mp)

  // Iterate the map and print out the elements one by one
  // Note: that mp has to be deferenced here or range will fail
  for key, value := range mp {
 fmt.Println("key:", key, "value:", value)


Hope you find this short tutorial useful in understanding Golang interface.

References :




  See also : Golang : Implementing class(object-oriented programming style)

Tags : golang interface unmarshal object-oriented

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.