codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

16 / 72 topics
11Arrays12Slices13Maps14Structs15Methods16Interfaces
Tutorials/Go (Golang)/Interfaces
🐹Go (Golang)

Interfaces

Updated 2026-05-15
10 min read

Interfaces

Introduction

In the world of programming, interfaces play a crucial role in defining contracts between different parts of your code. They allow you to specify what methods and properties an object must have without dictating how those methods should be implemented. This tutorial will guide you through the basics of defining and implementing interfaces in Go, providing both theoretical understanding and practical examples.

Concept

An interface in Go is a type that defines a set of method signatures. Any type that implements all these methods is said to implement the interface. Interfaces are used extensively in Go to achieve polymorphism, allowing functions to operate on different types as long as they satisfy the required interface.

Key Points:

  • Method Signatures: An interface lists the method names and their parameter and return types.
  • Implicit Implementation: A type automatically implements an interface if it has all the methods defined by that interface.
  • Empty Interface: The interface{} type is a special case known as the empty interface, which can hold values of any type.

Examples

Let's dive into some examples to understand how interfaces work in Go.

Example 1: Basic Interface Definition

First, let's define a simple interface called Shape that requires two methods: Area() and Perimeter().

Go
1package main
2
3import "fmt"
4
5// Define the Shape interface
6type Shape interface {
7 Area() float64
8 Perimeter() float64
9}
10
11// Implement the Shape interface for a Rectangle struct
12type Rectangle struct {
13 Width float64
14 Height float64
15}
16
17func (r Rectangle) Area() float64 {
18 return r.Width * r.Height
19}
20
21func (r Rectangle) Perimeter() float64 {
22 return 2 * (r.Width + r.Height)
23}
24
25// Implement the Shape interface for a Circle struct
26type Circle struct {
27 Radius float64
28}
29
30func (c Circle) Area() float64 {
31 return 3.14 * c.Radius * c.Radius
32}
33
34func (c Circle) Perimeter() float64 {
35 return 2 * 3.14 * c.Radius
36}
37
38// Function that works with any Shape
39func PrintShapeInfo(s Shape) {
40 fmt.Printf("Area: %.2f, Perimeter: %.2f
41", s.Area(), s.Perimeter())
42}
43
44func main() {
45 r := Rectangle{Width: 5, Height: 3}
46 c := Circle{Radius: 4}
47
48 PrintShapeInfo(r)
49 PrintShapeInfo(c)
50}

In this example:

  • We define a Shape interface with two methods: Area() and Perimeter().
  • We create two structs, Rectangle and Circle, and implement the Shape interface for both.
  • The PrintShapeInfo function takes any type that satisfies the Shape interface and prints its area and perimeter.

Example 2: Using Interfaces with Functions

Interfaces are particularly useful when working with functions. You can write functions that accept an interface, allowing them to work with multiple types.

Go
1package main
2
3import "fmt"
4
5// Define the Animal interface
6type Animal interface {
7 Speak() string
8}
9
10// Implement the Animal interface for a Dog struct
11type Dog struct{}
12
13func (d Dog) Speak() string {
14 return "Woof!"
15}
16
17// Implement the Animal interface for a Cat struct
18type Cat struct{}
19
20func (c Cat) Speak() string {
21 return "Meow!"
22}
23
24// Function that accepts an Animal and prints its sound
25func MakeAnimalSpeak(a Animal) {
26 fmt.Println(a.Speak())
27}
28
29func main() {
30 dog := Dog{}
31 cat := Cat{}
32
33 MakeAnimalSpeak(dog)
34 MakeAnimalSpeak(cat)
35}

In this example:

  • We define an Animal interface with a single method Speak().
  • We create two structs, Dog and Cat, and implement the Animal interface for both.
  • The MakeAnimalSpeak function takes any type that satisfies the Animal interface and prints its sound.

What's Next?

Now that you have a good understanding of interfaces in Go, the next step is to explore concurrency. Concurrency is a powerful feature in Go that allows you to write programs that can perform multiple tasks simultaneously. You'll learn how to use goroutines and channels to write concurrent code, which is essential for building scalable applications.

Stay tuned for more tutorials on Go's advanced features!


PreviousMethodsNext Concurrency Basics

Recommended Gear

MethodsConcurrency Basics