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
package main
import "fmt"
type AuthorDetails interface {
details()
}
type AuthorArticles interface {
articles()
}
type author struct {
a_name string
branch string
college string
year int
salary int
particles int
tarticles int
}
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)
}
func (a author) articles() {
pendingarticles := a.tarticles - a.particles
fmt.Printf("\nPending articles: %d", pendingarticles)
}
func main() {
values := author{
a_name: "Mickey",
branch: "Computer science",
college: "XYZ",
year: 2012 ,
salary: 50000 ,
particles: 209 ,
tarticles: 309 ,
}
var i1 AuthorDetails = values
i1.details()
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" }
var r Reader = doc
fmt.Println( "Content before writing:" , r.Read())
var w Writer = doc
w.Write( "New 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.