Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Embedding Interfaces in Golang
Next article icon

Embedding Interfaces in Golang

Last Updated : 16 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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
// Go program to illustrate the concept // of the embedding interfaces package main  import "fmt"  // Interface 1 type AuthorDetails interface {      details() }  // Interface 2 type AuthorArticles interface {      articles() }  // Interface 3  // Interface 3 embedded with // interface 1 and 2 type FinalDetails interface {      AuthorDetails     AuthorArticles }  // 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 methods of      // the interface 1 and 2     // Using FinalDetails interface     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
// Go program to illustrate the // concept of embedding interfaces package main  import "fmt"  // Interface 1 type AuthorDetails interface {     details() }  // Interface 2 type AuthorArticles interface {     articles() }  // Interface 3  // Interface 3 embedded with  // interface 1 and 2's methods type FinalDetails interface {      details()     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 methods      // of the interface 1 and 2     // Using FinalDetails interface     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
// Go program to illustrate the concept // of the embedding interfaces package main  import "fmt"  // Interface 1 type AuthorDetails interface {     details() }  // Interface 2 type AuthorArticles interface {     articles()     picked() }  // Interface 3 // Interface 3 embedded with interface  // 1's method and interface 2 // And also contain its own method type FinalDetails interface {     details()     AuthorArticles     cdeatils() }  // Structure type author struct {     a_name    string     branch    string     college   string     year      int     salary    int     particles int     tarticles int     cid       int     post      string     pick      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 methods  // of the interface 2 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) }  // Implementing the method // of the embedded interface func (a author) cdeatils() {      fmt.Printf("\nAuthor Id: %d", a.cid)     fmt.Printf("\nPost: %s", a.post) }  // 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,         cid:       3087,         post:      "Technical content writer",         pick:      58,     }      // Accessing the methods      // of the interface 1 and 2     // Using FinalDetails interface     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.


Next Article
Embedding Interfaces in Golang

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang

Similar Reads

    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
    Embedded Structures in Golang
    In the Go programming language, Embedded Structures are a way to reuse a struct's fields without having to inherit from it. It allows you to include the fields and methods of an existing struct into a new struct. The new struct can add additional fields and methods to those it inherits. Syntax: type
    2 min read
    Multiple Interfaces in Golang
    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 sig
    4 min read
    Inheritance in GoLang
    Inheritance means inheriting the properties of the superclass into the base class and is one of the most important concepts in Object-Oriented Programming. Since Golang does not support classes, so inheritance takes place through struct embedding. We cannot directly extend structs but rather use a c
    3 min read
    How to Access Interface Fields in Golang?
    Go language interfaces are different from other languages. In Go language, the interface is a custom type that is used to specify a set of one or more method signatures and the interface is abstract, so you are not allowed to create an instance of the interface. But you are allowed to create a varia
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences