Multiple Interfaces in Golang


Interfaces in Golang are an integral part of the language's design philosophy. They enable polymorphism, which is the ability to create objects with different underlying types but with common behavior. However, sometimes a struct needs to implement multiple interfaces. This is where multiple interfaces come into play.

In this article, we'll explore the concept of multiple interfaces in Golang, how to implement them, and their practical applications.

What are Multiple Interfaces in Golang?

In Golang, a type can implement multiple interfaces. When a struct implements multiple interfaces, it gains access to all the methods of those interfaces. This means that the struct can be used in any context where any of the implemented interfaces are expected.

Example

The following code snippet shows how a struct can implement multiple interfaces −

package main

import "fmt"

type Printer interface {
   Print()
}

type Saver interface {
   Save()
}

type Document struct {
   text string
}

func (d *Document) Print() {
   fmt.Println(d.text)
}

func (d *Document) Save() {
   // save the document to disk
}

func main() {
   doc := &Document{text: "Hello, world!"}
   var p Printer = doc
   var s Saver = doc
   p.Print()
   s.Save()
}

Output

Hello, world!

In this example, the Document struct implements both the Printer and Saver interfaces. We create a variable of type Printer and Saver and assign them to the same Document instance. The Print() method of Printer is called on the doc variable, and the Save() method of Saver is called on the same variable.

Benefits of Multiple Interfaces

Multiple interfaces offer several benefits in Golang −

  • Code Reusability − By implementing multiple interfaces, a struct can be used in multiple contexts, making it more reusable.

  • Code Flexibility − A struct that implements multiple interfaces can adapt to different situations, providing more flexibility in programming.

  • Modular Code − Interfaces provide a way to decouple code, making it easier to maintain and modify. By implementing multiple interfaces, a struct can be used in different modules of a program, promoting modularity.

Practical Applications of Multiple Interfaces

Multiple interfaces have practical applications in Golang. For example, the database/sql package in Golang uses multiple interfaces to provide access to different types of databases. The sql.DB struct implements the Conn and Execer interfaces, which enables it to be used with different types of databases.

Another example is the http package in Golang, which uses multiple interfaces to provide a standardized way of handling HTTP requests and responses. The http.ResponseWriter and http.Request structs implement multiple interfaces, which allows them to be used in different contexts.

Conclusion

Multiple interfaces in Golang provide a powerful way to create reusable, flexible, and modular code. They allow a struct to implement multiple behaviors, making it adaptable to different situations. By using multiple interfaces, developers can create code that is easier to maintain and modify.

Updated on: 25-Apr-2023

869 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements