---
title: "The concept of Go interfaces."  
description: "The concept of Go interfaces."  
author: "Utpal Vishwas"  
published: 2023-10-17  
updated: 2023-10-17  
canonical: https://www.mindstick.com/forum/160184/the-concept-of-go-interfaces  
category: "go"  
tags: ["interface", "go", "golang"]  
reading_time: 2 minutes  

---

# The concept of Go interfaces.

The [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) of Go interfaces.

## Replies

### Reply by Aryan Kumar

In Go, interfaces are a powerful and versatile feature that defines a set of methods that a type must implement. They enable polymorphism, allowing different types to be used interchangeably as long as they satisfy the interface's method requirements. Here's a humanized explanation of the concept of Go interfaces:

**Definition**:

- An interface in Go is like a contract that specifies a set of method signatures but doesn't provide the method implementations.
- It defines what a type can do without specifying how it does it.

**Method Requirement**:

- A type in Go is said to implement an interface if it provides method implementations for all the methods declared in that interface.
- There's no explicit "implements" keyword or declaration; if a type satisfies the interface methods, it's considered to implement that interface.

**Polymorphism**:

- Interfaces allow you to write code that works with objects of different types as long as they adhere to a common interface.
- This enables polymorphism, where different types can be used interchangeably, making your code more flexible and extensible.

**Duck Typing**:

- Go uses a form of duck typing, where "if it looks like a duck and quacks like a duck, it's a duck." In Go, it's more like "if it satisfies the interface, it's an instance of that interface."

**Empty Interfaces**:

- An empty interface, denoted as **interface{}**, can hold values of any type. It's used when you need flexibility in accepting various types as arguments or for returning different types.

Example:

```plaintext
// Defining an interface named Writer
type Writer interface {
    Write([]byte) (int, error)
}

// A struct that implements the Write method
type FileWriter struct {
    // Fields...
}

func (f FileWriter) Write(data []byte) (int, error) {
    // Implementation for writing to a file
    return len(data), nil
}

// A function that accepts a Writer interface
func WriteToFile(w Writer, data []byte) {
    w.Write(data)
}

func main() {
    file := FileWriter{} // Instantiate FileWriter
    data := []byte("Hello, Go!")

    WriteToFile(file, data) // Call the function with the FileWriter
}
```

In this example, we define an interface **Writer** with a **Write** method. The **FileWriter** struct implements the **Write** method, so it satisfies the **Writer** interface. The **WriteToFile** function can accept any type that implements the **Writer** interface, allowing flexibility and code reuse.


---

Original Source: https://www.mindstick.com/forum/160184/the-concept-of-go-interfaces

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
