DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Rapulu
Rapulu

Posted on

Basic Understanding of Golang Interface

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.

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
A PHP interface

Now let's build a class to implement this.

PHP Class Implements Interface

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.

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

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.

Top comments (4)

Collapse
 
heavykenny profile image
Kehinde Olawuwo.

Great piece.

Collapse
 
mlueckl profile image
Michael Lueckl

Thank you for this great explanation! :)

Collapse
 
rapulu profile image
Rapulu Author

You are welcome.

Collapse
 
mombe090 profile image
Mamadou Yaya Diallo

Great article

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈