Basic Understanding of Golang Interface
Tags: go, beginners
Published: 2019-02-21 00:06:44

---
title: Basic Understanding of Golang Interface
published: true
description: This article is a beginner's guide to understanding interface in Golang, and its implicit implementation.
tags: go, beginner
---

As a beginner in Go and someone coming from language that explicitly implements interface, it took me time to understand, I would also explain why it is said to be implicitly implemented.

__What is an interface from golang point of view?__

An interface is a collection of method signature(s) that an object (e.g struct or non-struct type) can implicitly implement. Hence interfaces allow to specify that only some behaviour is needed, the behaviour is defined by the set of methods.


__Understanding Interface Method Signatures__

The first sentence states "*An interface is a collection of method signatures*" if you are new to programming you might find this a bit confusing as it was to me at a time, so let me graphically explain what that means.

```go
package main

import "fmt"

type Walker interface{// Interface type to be implemented

    Walk() string // This is a method signature inside an interface
    Bark() string // Another method signature
}

func main(){
    fmt.Println("Interface method signatures")
}
```
The code above declared an interface type with two methods signature to be implemented implicitly.

__Explicit implement__

In order to fully understand how an object can implicitly implement an interface, let's look at it from another language that explicitly implements interface like PHP.

![PHP Interface](https://thepracticaldev.s3.amazonaws.com/i/poi0dcgqb9dy951mif8b.png)
*A PHP interface*

 Now let's build a class to implement this.

![PHP Class Implements Interface](https://thepracticaldev.s3.amazonaws.com/i/sbhi24y9k1wz5jee0lrr.png)

Every method present in the interface must be implemented by the class. The class must respect entirely the interface like it was a rigid contract. This is an example of an explicit interface, wherein the class is stated what interface to implement.

__Implicit Implement__

We have seen how PHP explicitly implements interface, let's look at Go implicit way.
You do not explicitly mention if a type implements an interface. If a Type implements a method of signature that is defined in an interface, then that Type is said to implement that interface.

```go
package main

import "fmt"

type Walker interface{
    Walk() string// This is a method signature inside an interface
}

//Non-struct Type to implement the walker interface
type Dog string

//create the method signature Walk
func (dog Dog) Walk() string{
    return "Dog can walk"
}

func main(){
    var dog Dog
    callWalker(dog)
}

func callWalker(w Walker){
    fmt.Println(w.Walk())
}
    
```
 [Run this code here](https://play.golang.org/p/_nHflMDe0fi)

Notice I didn’t specify anything in the Type Dog about the Walker?

This is an implicit implementation of an interface, the important thing is that the Type Dog has a Walk method with the signature in Walker interface.

This was a bit confusing for me to interiorize when I was learning interface on Go but in the end, was probably the major factor to fall in love with this language.

I will stop here, trying to make this as short as possible so you don't get bored.

P.S Contributions are highly welcomed by anyone.