The Wayback Machine - https://web.archive.org/web/20240204104952/https://www.geeksforgeeks.org/promoted-fields-in-golang-structure/
Open In App
Related Articles

Promoted Fields in Golang Structure

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 the Anonymous Field to the nested structure. And the fields of that structure (other than nested structure) are the part of the nested structure, such type of fields are known as Promoted fields. If the anonymous structure or nested structure and parent structure contains a field that has the same name, then that field doesn’t promote, only different name fields get promoted to the structure.

Syntax:

type x struct{
// Fields
}

type y struct{
// Fields of y structure
x
}

Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate the
// concept of the promoted fields
package main
  
import "fmt"
  
// Structure
type details struct {
  
    // Fields of the
    // details structure
    name   string
    age    int
    gender string
}
  
// Nested structure
type student struct {
    branch string
    year   int
    details
}
  
func main() {
  
    // Initializing the fields of
    // the student structure
    values := student{
        branch: "CSE",
        year:   2010,
        details: details{
          
            name:   "Sumit",
            age:    28,
            gender: "Male",
        },
    }
  
    // Promoted fields of the student structure
    fmt.Println("Name: ", values.name)
    fmt.Println("Age: ", values.age)
    fmt.Println("Gender: ", values.gender)
  
    // Normal fields of
    // the student structure
    fmt.Println("Year: ", values.year)
    fmt.Println("Branch : ", values.branch)
}


Output:

Name:  Sumit
Age:  28
Gender:  Male
Year:  2010
Branch :  CSE

Explanation: In the above example, we have two structures named as details and student. Where details structure is the normal structure and student structure is the nested structure which contains the details structure as fields in it just like anonymous fields. Now, the fields of the details structure, i.e, name, age, and gender are promoted to the student structure and known as promoted fields. Now, you can directly access them with the help of the student structure like values.name, values.age, and values.gender.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 13 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads