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:
Promoted Methods in Golang Structure
Next article icon

Promoted Methods in Golang Structure

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Go structure, the working of promoted methods is just like Promoted Fields. We use this concept in the nested structure where a structure is a field in another structure, simply by just adding the name of the structure into another structure and it behaves like the Anonymous Field to the nested structure. And the methods of that structure (other than nested structure) are the part of the nested structure, such type of methods are known as Promoted Methods. Or in other words, prompted methods are those methods which are implemented by the child structure, and is accessible by the parent structure.

Important Points:

  • If the child and the parent structure contain a method that has the same name but different type of receiver, then both the methods are available in the parent structure as shown in Example 2. Here both child and parent structure contain methods of the same names.
  • If child structure contains two methods with the same name and same receiver, then these methods are not promoted in the parent structure and if you try to do, then the compiler will give an error.

Example 1:

C
// Golang program to illustrate the // concept of the promoted methods package main  import "fmt"  // Structure type details struct {      // Fields of the     // details structure     name    string     age     int     gender  string     psalary int }  // Nested structure type employee struct {     post string     eid  int     details }  // Method func (d details) promotmethod(tsalary int) int {     return d.psalary * tsalary }  func main() {      // Initializing the fields of     // the employee structure     values := employee{         post: "HR",         eid:  4567,         details: details{              name:    "Sumit",             age:     28,             gender:  "Male",             psalary: 890,         },     }      // Promoted fields of the      // employee structure     fmt.Println("Name: ", values.name)     fmt.Println("Age: ", values.age)     fmt.Println("Gender: ", values.gender)     fmt.Println("Per day salary: ", values.psalary)      // Promoted method of the     // employee structure     fmt.Println("Total Salary: ", values.promotmethod(30))      // Normal fields of     // the employee structure     fmt.Println("Post: ", values.post)     fmt.Println("Employee id: ", values.eid) } 

Output:

Name:  Sumit Age:  28 Gender:  Male Per day salary:  890 Total Salary:  26700 Post:  HR Employee id:  4567 

Example 2:

C
// Golang program to illustrate the // concept of the promoted methods package main  import "fmt"  // Structure type details struct {      // Fields of the     // details structure     name    string     age     int     gender  string     psalary int }  // Method 1 func (e employee) promotmethod(tarticle int) int {     return e.particle * tarticle }  // Nested structure type employee struct {     post     string     particle int     eid      int     details }  // Method 2 func (d details) promotmethod(tsalary int) int {     return d.psalary * tsalary }  // Main method func main() {      // Initializing the fields of     // the employee structure     values := employee{         post:     "HR",         eid:      4567,         particle: 5,         details: details{              name:    "Sumit",             age:     28,             gender:  "Male",             psalary: 890,         },     }      // Promoted fields of     // the employee structure     fmt.Println("Name: ", values.name)     fmt.Println("Age: ", values.age)     fmt.Println("Gender: ", values.gender)     fmt.Println("Per day salary: ", values.psalary)      // Promoted method of      // the employee structure     fmt.Println("Total Salary: ", values.details.promotmethod(30))      // Normal fields of     // the employee structure     fmt.Println("Post: ", values.post)     fmt.Println("Employee id: ", values.eid)     fmt.Println("Total Articles: ", values.promotmethod(30)) } 

Output:

Name:  Sumit Age:  28 Gender:  Male Per day salary:  890 Total Salary:  150 Post:  HR Employee id:  4567 Total Articles:  150 

Next Article
Promoted Methods in Golang Structure

A

ankita_saini
Improve
Article Tags :
  • Go Language

Similar Reads

    Nested Structure in Golang
    A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct. Go language allows nested structure. A structure which is the
    3 min read
    Promoted Fields in Golang Structure
    In Go structure, promoted fields are just like anonymous fields, the type of the field is the name of the field. We use this concept in the nested structure where a structure is a field in another structure, simply by just adding the name of the structure into another structure and it behaves like t
    2 min read
    Structures in Golang
    A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-orient
    7 min read
    Pointer to a Struct in Golang
    In Go, structs are used to create custom data types that group different fields together. When working with structs, using pointers can be especially beneficial for managing memory efficiently and for avoiding unnecessary copying of data. A pointer to a struct allows you to directly reference and mo
    3 min read
    Methods in Golang
    Go methods are like functions but with a key difference: they have a receiver argument, which allows access to the receiver's properties. The receiver can be a struct or non-struct type, but both must be in the same package. Methods cannot be created for types defined in other packages, including bu
    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