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 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.


type x struct{
// Fields

type y struct{
// Fields of y structure

Let us discuss this concept with the help of an 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
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: ",
    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)



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.age, and values.gender.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.