Golang program to call one constructor from another


In Go programming language, a constructor is a specific kind of function used to initialize an object's state when it is initially formed. It is used to initialize variables and provide data for the objects. We will execute this program using two methods and we will use struct in both of these examples with anonymous struct in the second example. Let’s see through these examples to understand how the program is executed.

Method 1: Using a function that returns a desired type

In this method, to construct a new Child struct, the NewChildWithAge function runs the NewChildfunction, sets the age, and returns the finished 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 − Construct a Child struct with the properties Name and Age.

  • Step 3 − Make a function called NewChild that accepts a name string as input and returns a pointer to a structure called Child. The Name field of the Child struct is initialized by this function to the input name, while the Age field is initialized to 0.

  • Step 4 − Make a function called NewChildWithAge that accepts a name string and an age int as inputs and returns a pointer to a structure called Child.

  • Step 5 − Call the NewChild function to create a new Child struct in the NewChildWithAge function.

  • Step 6 − Set the age input to the Age field of the Child struct returned by NewChild.

  • Step 7 − Return Child struct back and Call the NewChildWithAge function in the main function, passing the parameters "Virat" and 10 as input.

  • Step 8 − In a variable named c, save the resulting Child struct.

  • Step 9 − Print the Child struct's Name and Age fields using fmt.Println() function where ln means new line.

Example

In this example, we will use a function that returns a desired type. Let’s have a look at the code.

package main
import "fmt"

type Child struct {
   Name string
   Age int
}

func NewChild(name string) *Child {
   return &Child{   
      Name: name,
      Age:  0,
   }
}

func NewChildWithAge(name string, age int) *Child {
   c := NewChild(name)     //call this function to create a new child struct
   c.Age = age
   return c  //return the child attributes
}

func main() {
   c := NewChildWithAge("Virat", 10)
   fmt.Println("Here, calling one constructor from another can be represented as:")
   fmt.Println(c.Name, c.Age)  //print he name and age of child
}

Output

Here, calling one constructor from another can be represented as:
Virat 10

Method 2: Using an anonymous struct field

This method has an anonymous field of type Child, allowing it to inherit all of the attributes of Child. By using the NewChild function to initialize the Child field and setting the Age field to the supplied age, the NewChildWithAge method constructs a new ChildWithAge 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 − Make a Child structure using only the Name field.

  • Step 3 − Make a ChildWithAge struct with two fields: an Age field and an anonymous Person field.

  • Step 4 − Make a function called NewChild that accepts a name string as input and returns a pointer to a structure called Child. The Name field of the Child struct is initialized by this function using the provided name.

  • Step 5 − Create a function called NewChildWithAge that accepts a name string and an age int as inputs and returns a pointer to a structure called ChildWithAge.

  • Step 6 − Initialize a fresh ChildWithAge struct in the NewChildWithAge function.

  • Step 7 − Use the NewChild function to initialize the ChildWithAge struct's anonymous Person field.

  • Step 8 − Set the input age in the ChildWithAge struct's Age field.

  • Step 9 − Return the ChildWithAge struct back to the function and call the NewChildWithAge function in the main function, passing the parameters "Virat" and 10 as input.

  • Step 10 − In a variable named c, save the resulting ChildWithAge struct.

  • Step 11 − Print the ChildWithAge struct's Name and Age attributes.

Example

In this example, we will use an anonymous struct field to call one constructor from another. Let’s see through the code.

package main
import "fmt"

type Child struct {
   Name string
   Age  int
}

type ChildWithAge struct {
   Child
   Age int
}

func NewChild(name string) *Child {
   return &Child{
      Name: name,
   }
}

func NewChildWithAge(name string, age int) *ChildWithAge {
   return &ChildWithAge{
      Child: *NewChild(name),  //call the function to create a child struct
      Age:   age,
   }
}

func main() {
   c := NewChildWithAge("Virat", 10)  //set the values and store the output in c
   fmt.Println("Here, calling one constructor from another can be represented as:")
   fmt.Println(c.Name, c.Age) //print the name and age of child
}

Output

Here, calling one constructor from another can be represented as:
Virat 10

Conclusion

We executed the program by calling one constructor from another using two methods. In the first method, we used a function that returns a desired type and in the second method, we used an anonymous struct to execute the program.

Updated on: 20-Feb-2023

559 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements