Multiple Interfaces in Golang
Last Updated :
11 Apr, 2023
In Go language, the interface is a collection of method signatures and it is also a type means you can create a variable of an interface type. In Go language, you are allowed to create multiple interfaces in your program with the help of the given syntax:
type interface_name interface{
// Method signatures
}
Note: In Go language, you are not allowed to create same name methods in two or more interfaces. If you try to do so, then your program will panic. Let us discuss multiple interfaces with the help of an example. Example:
Go
// Go program to illustrate the
// concept of multiple interfaces
package main
import "fmt"
// Interface 1
type AuthorDetails interface {
details()
}
// Interface 2
type AuthorArticles interface {
articles()
}
// Structure
type author struct {
a_name string
branch string
college string
year int
salary int
particles int
tarticles int
}
// Implementing method
// of the interface 1
func (a author) details() {
fmt.Printf("Author Name: %s", a.a_name)
fmt.Printf("\nBranch: %s and passing year: %d", a.branch, a.year)
fmt.Printf("\nCollege Name: %s", a.college)
fmt.Printf("\nSalary: %d", a.salary)
fmt.Printf("\nPublished articles: %d", a.particles)
}
// Implementing method
// of the interface 2
func (a author) articles() {
pendingarticles := a.tarticles - a.particles
fmt.Printf("\nPending articles: %d", pendingarticles)
}
// Main value
func main() {
// Assigning values
// to the structure
values := author{
a_name: "Mickey",
branch: "Computer science",
college: "XYZ",
year: 2012,
salary: 50000,
particles: 209,
tarticles: 309,
}
// Accessing the method
// of the interface 1
var i1 AuthorDetails = values
i1.details()
// Accessing the method
// of the interface 2
var i2 AuthorArticles = values
i2.articles()
}
Output:
Author Name: Mickey
Branch: Computer science and passing year: 2012
College Name: XYZ
Salary: 50000
Published articles: 209
Pending articles: 100
Explanation: As shown in the above example we have two interfaces with methods, i.e, details() and articles(). Here, details() method provides the basic details of the author and articles() method provides the pending articles of the author.
And a structure named as an author which contains some set of variables whose values are used in the interfaces. In the main method, we assign the values of the variables present, in the author structure, so that they will use in the interfaces and create the interface type variables to access the methods of the AuthorDetails and AuthorArticles interfaces.
Here's an example code demonstrating multiple interfaces in Go:
Go
package main
import "fmt"
type Reader interface {
Read() string
}
type Writer interface {
Write(string)
}
type ReadWriter interface {
Reader
Writer
}
type Document struct {
content string
}
func (d Document) Read() string {
return d.content
}
func (d *Document) Write(content string) {
d.content = content
}
func main() {
doc := &Document{content: "Initial content"}
// using the Reader interface to read the document content
var r Reader = doc
fmt.Println("Content before writing:", r.Read())
// using the Writer interface to write new content to the document
var w Writer = doc
w.Write("New content")
// using the ReadWriter interface to read the updated document content
var rw ReadWriter = doc
fmt.Println("Content after writing:", rw.Read())
}
Output:
Content before writing: Initial content
Content after writing: New content
In this example, we define three interfaces: Reader, Writer, and ReadWriter. The ReadWriter interface embeds both the Reader and Writer interfaces.
We then define a Document struct that has a content string field. We implement the Read method on the Document type to return the value of the content field, and the Write method to update the content field with a new value.
In the main function, we create a new Document instance and assign it to the doc variable. We then use the Reader interface to read the initial content of the document, followed by the Writer interface to write new content to the document. Finally, we use the ReadWriter interface to read the updated content of the document.
Similar Reads
Go Tutorial
Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google
2 min read
Go Programming Language (Introduction)
Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env
11 min read
time.Sleep() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Sleep() function in Go language is used to stop the latest go-routine for at least the stated duration d. And a negative or zero duration of sleep will cause this method to return instantly. Moreover, t
3 min read
Learn Free Programming Languages
In this rapidly growing world, programming languages are also rapidly expanding, and it is very hard to determine the exact number of programming languages. Programming languages are an essential part of software development because they create a communication bridge between humans and computers. No
9 min read
Golang Tutorial - Learn Go Programming Language
This Golang tutorial provides you with all the insights into Go Language programming, Here we provide the basics, from how to install Golang to advanced concepts of Go programming with stable examples. So, if you are a professional and a beginner, this free Golang tutorial is the best place for your
10 min read
strings.Contains Function in Golang with Examples
strings.Contains Function in Golang is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false. Syntax:Â func Contains(str, substr string) bool Here, str is the original string and substr is t
2 min read
Interfaces in Golang
In Go, an interface is a type that lists methods without providing their code. You canât create an instance of an interface directly, but you can make a variable of the interface type to store any value that has the needed methods.Exampletype Shape interface { Area() float64 Perimeter() float64}In t
3 min read
fmt.Sprintf() Function in Golang With Examples
In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Sprintf() function in Go language formats according to a format specifier and returns the resulting string. Moreover, this function is defined under the fmt package. Here, you
2 min read
Top 10 Golang Project Ideas with Source Code in 2025
Golang, or Go, a programming language was created by Google. It's widely used for building different kinds of applications like websites and cloud services. The fastest way to master this language is by building projects related to it. This article introduces 10 beginner-friendly to medium-difficult
8 min read
Top 10 Golang Frameworks in 2025
Golang (or Go) is an open-source compiled programming language that is used to build simple, systematic, and secure software. It was designed by Google in the year 2007 and has been readily adopted by developers all over the world due to its features like memory safety, structural typing, garbage co
9 min read