Interfaces in Go

What are Interfaces in Go?

March 30th, 2026
2256
8:00 Minutes

The interfaces in Go are a type which describes a set of method signatures. It describes a contract for behavior without giving the implementation details. In this blog we will understand in detail about what are interfaces in Go, why they are useful, understanding the power of Go interfaces, its types and much more.

What are Interfaces in Go?

Go Interfaces describe a set of method signatures without giving an implementation. A solid type, like 'struct', implicitly puts an interface by easily possessing all the methods declared by that interface. This permits flexibility, reusability and decoupled code. Unlike a few other programming languages, interfaces in Go do not use an explicit 'implements' keyword.

What are Interfaces in Go

Why are Go Interfaces Useful?

Interfaces in Go are useful for many kinds of reasons that you may end up using a Go interface.

Here are the reasons why Go interfaces are useful.

  • It makes it easier to make use of mocks instead of real objects in unit tests.
  • It is an architecture tool for helping enforce decoupling between parts of your codebase.

Understanding the Power of Go Interfaces

Go Interfaces lets you write code which is generic and can be put to use with any kind which satisfies the interface. Let us understand the power of interfaces in Go, in depth with examples.

package main

import "fmt"

// Define an interface

type Speaker interface {

Speak() string

}

// First struct implementing the interface

type Person struct {

Name string

}

// Person's Speak method

func (p Person) Speak() string {

return "Hello, I'm " + p.Name

}

// Second struct implementing the interface

type Robot struct {

ID int

}

// Robot's Speak method

func (r Robot) Speak() string {

return fmt.Sprintf("Beep boop, ID %d", r.ID)

}

// Function that uses the interface

func makeSound(s Speaker) {

fmt.Println(s.Speak())

}

func main() {

person := Person{Name: "Alice"}

robot := Robot{ID: 42}

makeSound(person) // Output: Hello, I'm Alice

makeSound(robot) // Output: Beep boop, ID 42

}

Here is the explanation of the example given above:

  • Interface Definition: The Speaker interface defines a single method, Speak(), that returns a string. Any type implementing this method satisfies the Speaker interface.
  • Structs: Person and Robot are two different structs, each with its own Speak() method, making them implement the Speaker interface implicitly.
  • Polymorphism: The makeSound function accepts any Speaker type, allowing it to work with both Person and Robot.
  • Usage: In main, we create a Person and a Robot, then pass them to makeSound, which calls their respective Speak methods.

The above example shows how Go interfaces allow flexibility and reusability of code without explicit inheritance.

Want to Master Golang Programming Language? 

Explore our Golang Tutorial to boost your coding skills and gain hands-on knowledge in Go Programming.

Explore Now
Golang Learning Illustration

Types of Interfaces in Go: Explained with Examples

The interfaces in Go are a set of method signatures. Any type which is implemented on those methods are said to 'satisfy' the interface, and any kind of explicit declaration is not required. Below given, are the types of Go interfaces explained with examples of their own.

1. Basic Interface

It defines one or more methods. Any type that implements those methods satisfies the interface.

package main

import "fmt"

type Speaker interface {

Speak() string

}

type Dog struct{}

func (d Dog) Speak() string {

return "Woof!"

}

func main() {

var s Speaker = Dog{}

fmt.Println(s.Speak()) // Output: Woof!

}

Here, any type with Speak() satisfies the Speaker.

2. Empty Interface (interface{} or any)

Empty Interface represents zero methods. Every type satisfies it, so it can hold any value.

package main

import "fmt"

func PrintAnything(v interface{}) {

fmt.Println(v)

}

func main() {

PrintAnything(42) // int

PrintAnything("hello") // string

PrintAnything(3.14) // float

}

It is usually used for generic containers, but in Go 1.18+ any is preferred.

3. Embedded Interfaces

An interface can include other interfaces, creating a bigger interface.

package main

import "fmt"

type Reader interface {

Read() string

}

type Writer interface {

Write(s string)

}

type ReadWriter interface {

Reader

Writer

}

type File struct {

content string

}

func (f *File) Read() string { return f.content }

func (f *File) Write(s string) { f.content = s }

func main() {

var rw ReadWriter = &File{}

rw.Write("Hello Go!")

fmt.Println(rw.Read()) // Output: Hello Go!

}

Here, ReadWriter is just Reader + Writer.

4. Interface with Multiple Implementations

The different types can implement the same interface in their own way.

package main

import "fmt"

type Shape interface {

Area() float64

}

type Circle struct{ Radius float64 }

type Square struct{ Side float64 }

func (c Circle) Area() float64 { return 3.14 * c.Radius * c.Radius }

func (s Square) Area() float64 { return s.Side * s.Side }

func main() {

shapes := []Shape{Circle{5}, Square{4}}

for _, shape := range shapes {

fmt.Println(shape.Area())

}

}

Both the Circle and Square implement Shape.

5. Nil Interface

An interface which has no value assigned is nil.

package main

import "fmt"

type Printer interface {

Print()

}

func main() {

var p Printer // no value assigned

if p == nil {

fmt.Println("Interface is nil")

}

}

It is useful for checking if an implementation was provided.

Read Also: Top 50+ Golang Interview Questions And Answers

How to Use Interfaces in Go?

To implement an interface in Go, a type must describe all methods declared by the interface. Implementation is implicit which means no keyword like 'implements' is required. Let us understand how to use Go interfaces with an example.

  • Describe an interface- a chain of methods.
  • Create a type- It must implement those methods.
  • Assigning the type to the interface- as it satisfies the contract.
  • Make use of the interface- One does not care 'which type' is behind it, just that it has the methods.

Here is an example to explain how to use Go interfaces.

package main

import "fmt"

// Step 1: Define an interface

type Animal interface {

Speak() string

}

// Step 2: Create types that implement the interface

type Dog struct{}

type Cat struct{}

func (d Dog) Speak() string { return "Woof!" }

func (c Cat) Speak() string { return "Meow!" }

// Step 3 & 4: Use the interface

func MakeItSpeak(a Animal) {

fmt.Println(a.Speak())

}

func main() {

d := Dog{}

c := Cat{}

MakeItSpeak(d) // Output: Woof!

MakeItSpeak(c) // Output: Meow!

}

Here is the explanation on what is happening here.

  • Animal says: “Anything that has Speak() string can be called an Animal.”
  • Dog and Cat both implement Speak(), so they automatically satisfy Animal.
  • MakeItSpeak doesn't care if it's a dog or a cat, it just calls Speak().

Declaration and Implementation of Interfaces in Go

In declaration of interfaces, an interface is declared through the type keyword followed by

In Go, an interface is declared using the type keyword followed by a name and a set of method signatures.

type InterfaceName interface {

MethodName(paramType) returnType

}

This is just a contract: “Any type that has these methods is part of this interface.”

Implementation of Interfaces

It is apart from other languages, Go does not use implements or extends. A type automatically implements an interface if it has all the required methods.

For example: Declaration + Implementation

package main

import "fmt"

Step 1: Declare the interface

type Shape interface {

Area() float64

}

Step 2: Create types

type Circle struct{ Radius float64 }

type Square struct{ Side float64 }

Step 3: Implement the interface methods

func (c Circle) Area() float64 {

return 3.14 * c.Radius * c.Radius

}

func (s Square) Area() float64 {

return s.Side * s.Side

}

Step 4: Use the interface

func printArea(sh Shape) {

fmt.Println("Area:", sh.Area())

}

func main() {

c := Circle{Radius: 5}

s := Square{Side: 4}

printArea(c) // Circle implements Shape

printArea(s) // Square implements Shape

}

Hope, these above examples helped you understand the declaration and implementation of interfaces in Go.

Wrapping Up

Go interfaces are a strong feature of the Go programming languages which allows polymorphism and elasticity in your code. Through describing interfaces, you can write code which is more modular, easy to test and easier to extend. As we have covered a lot in this article, involving what interfaces are, how to use them and some examples of how they are put to use in famous Go packages.

FAQs

Q1. Is Go language easy to learn?

Absolutely yes it's easy to learn, as it has a small syntax as compared to other programming languages and only has a minimalistic set of features to get the work done.

Q2. Is Go faster than Java?

Usually, Go is faster than Java for specific types of applications, basically those which need a lot of concurrencies.

Q3. Can interfaces have constructors?

No, an interface can't have a constructor, as it can't be put to use to make objects.

Q4. Why are interfaces important in Go?

Interfaces allow different types to work together in a flexible way. They make programs easier to maintain and extend.

Q5. What is a practical use of interfaces?

Interfaces are used for abstraction, allowing programs to work with different types in a uniform way.

Course Schedule

Course NameBatch TypeDetails
Golang Training
Every WeekdayView Details
Golang Training
Every WeekendView Details
About the Author
Piyush Verma | igmGuru
About the Author

Piyush is a technical writer skilled in Golang, R, C, C#, C++, Ruby, and ERP systems. He simplifies complex coding concepts into clear, beginner-friendly content, helping readers build strong foundations. With a structured approach, he supports both beginners and professionals in mastering technologies and advancing their careers.

Drop Us a Query
Fields marked * are mandatory

Programming Certification Courses

×

Your Shopping Cart


Your shopping cart is empty.