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.
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.

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.
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:
The above example shows how Go interfaces allow flexibility and reusability of code without explicit inheritance.
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.
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.
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.
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.
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.
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
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.
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.
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.”
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" |
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 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.
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.
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.
Usually, Go is faster than Java for specific types of applications, basically those which need a lot of concurrencies.
No, an interface can't have a constructor, as it can't be put to use to make objects.
Interfaces allow different types to work together in a flexible way. They make programs easier to maintain and extend.
Interfaces are used for abstraction, allowing programs to work with different types in a uniform way.
Course Schedule
| Course Name | Batch Type | Details |
| Golang Training | Every Weekday | View Details |
| Golang Training | Every Weekend | View Details |