Golang program to depict use of promoted fields


In golang, promoted fields are the structs that are embedded inside another structs and their fields can also be accessed the structs in which they are embedded. In this article we are going to understand three different approaches to depict use of promoted filed with and without the help of pointers.

Using Pointers

In this illustration, we will make use of pointers to show the promoted fields. The field names will be set with the help of pointers and information will be printed using dot notation.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a Person struct with two fields name and age of type string and int

  • Step 3 − Then, create an Employee struct with a promoted field *Person and id of type integer which means the fields of Person can be accessed via pointer

  • Step 4 − Create a main function and set the field values in the Person struct and then assign the personal struct values to the Employee struct using pointer

  • Step 5 − Print all the employee information on the console using Println function from the fmt package

Example

The following example demonstrates how to create Golang program to depict use of promoted fields using pointers

package main

import "fmt"

type Person struct {
   name string
   age  int
}

type Employee struct {
   *Person 
   id int
}

func main() {
   ps := &Person{name: "Geneva", age: 28} 
   emp_info := &Employee{Person: ps, id: 442} 

   fmt.Println("The Employee name is:", emp_info.name) 
   fmt.Println("The Employee age is:", emp_info.age)   
   fmt.Println("The Employee id is:", emp_info.id) 
}

Output

The Employee name is: Geneva
The Employee age is: 28
The Employee id is: 442

Without Using Pointers

In this method, a person and employee struct will be created in which the employee struct will contain the promoted field whose field’s value will be set in the main function and printed using dot with fields.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a Person struct with two fields name and age of type string and int

  • Step 3 − Then, create an Employee struct with two fields an id of type int and person is a promoted field in the struct.

  • Step 4 − Now, create a main function and in that function create an employee struct and provide the values to the name and age of the promoted field Person and also assign value to the id

  • Step 5 − Finally access the name and age of the Person with emp_info.name, emp_info.age and emp_info.id syntax.

  • Step 6 − Print all the above stated information on the console using Println function from the fmt package where ln refers to new line..

Example

In the following example we will understand how to create Golang program to depict use of promoted fields without using pointers.

package main

import "fmt"

type Person struct {
   name string
   age  int
}

type Employee struct {
   Person // Promoted field
   id  int
}

func main() {
   emp_info := Employee{
      Person: Person{
         name: "Veronica", 
         age:  36,
      },
      id: 600,
   }

   fmt.Println("The Employee name is:", emp_info.name) 
   fmt.Println("The Employee age is:", emp_info.age) 
   fmt.Println("The Employee id is:", emp_info.id) 
}

Output

The Employee name is: Veronica
The Employee age is: 36
The Employee id is: 600

Using the Animal and Dog Struct

In this example, we will write a Golang program to show use of promoted fields using the Animal and Dog struct. The Animal struct is the promoted field in the Dog struct.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create an Animal struct with two fields name of type string and age of type int

  • Step 3 − Then, create a Dog struct which contains a Promoted field Animal and another field Breed of type string.

  • Step 4 − Create a main function in which set the values of promoted fields and store it inside the Dog_struct

  • Step 5 − Access the fields using the storing variable with dot notation.

  • Step 6 − Then, access the promoted fields using the same notation.

  • Step 7 − The print statement is executed using the Println function from the fmt package where ln means new line.

Example

The following example shows how to create Golang program to show use of promoted fields using the Animal and Dog struct.

package main

import "fmt"

type Animal struct {
   name string
   age  int
}

type Dog struct {
   Animal
   Breed string
}

func main() {
   Dog_struct := Dog{
      Animal: Animal{
         name: "pommy",
         age:  6,
      },
      Breed: "Labrador retriever",
   }	
   fmt.Println(Dog_struct.name)
   fmt.Println(Dog_struct.age)
   fmt.Println(Dog_struct.Breed)	
   fmt.Println(Dog_struct.Animal.name)
   fmt.Println(Dog_struct.Animal.age)
}

Output

pommy
6
Labrador retriever
pommy
6

Conclusion

We compiled and executed the program of depicting promoted fields using three examples. In the first example, promoted field values were set in the struct whereas in the second example they were set with the help of pointers and in the third example we used an Animal struct.

Updated on: 04-Apr-2023

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements