Embedding Interfaces in Golang
Last Updated :
16 Jan, 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. As we know that the Go language does not support inheritance, but the Go interface fully supports embedding. In embedding, an interface can embed other interfaces or an interface can embed other interface’s method signatures in it, the result of both is the same as shown in Example 1 and 2. You are allowed to embed any number of interfaces in a single interface. And when an interface, embed other interfaces in it if we made any changes in the methods of the interfaces, then it will reflect in the embedded interface also, as shown in Example 3.
Syntax:
type interface_name1 interface {
Method1()
}
type interface_name2 interface {
Method2()
}
type finalinterface_name interface {
interface_name1
interface_name2
}
or
type interface_name1 interface {
Method1()
}
type interface_name2 interface {
Method2()
}
type finalinterface_name interface {
Method1()
Method2()
}
Example 1:
Go
package main
import "fmt"
type AuthorDetails interface {
details()
}
type AuthorArticles interface {
articles()
}
type FinalDetails interface {
AuthorDetails
AuthorArticles
}
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 f FinalDetails = values
f.details()
f.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 three interfaces. Interface 1 and 2 are simple interfaces and interface 3 is the embedded interface which holds both 1 and 2 interfaces in it. So if any changes take place in the interface 1 and interface 2 will reflect in interface 3. And interface 3 can access all the methods present in interface 1 and 2. Example 2:
Go
package main
import "fmt"
type AuthorDetails interface {
details()
}
type AuthorArticles interface {
articles()
}
type FinalDetails interface {
details()
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 f FinalDetails = values
f.details()
f.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 three interfaces. Interface 1 and 2 are simple interfaces and interface 3 is the embedded interface which holds both 1 and 2 interfaces method signatures in it. So if any changes take place in the interface 1 and interface 2’s methods it will not reflect in interface 3 until we define explicitly in interface 3. And interface 3 can access all the methods present in interface 1 and 2 only if those mentioned in interface 3. Example 3:
C
package main
import "fmt"
type AuthorDetails interface {
details()
}
type AuthorArticles interface {
articles()
picked()
}
type FinalDetails interface {
details()
AuthorArticles
cdeatils()
}
type author struct {
a_name string
branch string
college string
year int
salary int
particles int
tarticles int
cid int
post string
pick 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 (a author) picked() {
fmt.Printf("\nTotal number of picked articles: %d", a.pick)
}
func (a author) cdeatils() {
fmt.Printf("\nAuthor Id: %d", a.cid)
fmt.Printf("\nPost: %s", a.post)
}
func main() {
values := author{
a_name: "Mickey",
branch: "Computer science",
college: "XYZ",
year: 2012,
salary: 50000,
particles: 209,
tarticles: 309,
cid: 3087,
post: "Technical content writer",
pick: 58,
}
var f FinalDetails = values
f.details()
f.articles()
f.picked()
f.cdeatils()
}
|
Output:
Author Name: Mickey
Branch: Computer science and passing year: 2012
College Name: XYZ
Salary: 50000
Published articles: 209
Pending articles: 100
Total number of picked articles: 58
Author Id: 3087
Post: Technical content writer
Explanation: As shown in the above example we have three interfaces. Interface 1 and 2 are simple interfaces and interface 3 is the embedded interface which holds both interface 1’s method signatures, interface 2 and it’s own method in it. So if any changes take place in the interface 2’s method it will reflect in interface 3. And interface 3 can access all the methods present in it including interface 2. We can only access interface 1’s method which signature define in interface3.