Golang program to show promoted methods


In golang, Promoted methods are the methods created under those structs which are embedded inside another struct, now the struct in which it is embedded can access its methods and fields. In this article we are going to explain how to show promoted methods using various structs like Rectangle and Square, Vehicle and car.

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 Rectangle struct with two fields width and height of type float

  • Step 3 − Then, create a method named Area() which calculates the area of the rectangle

  • Step 4 − Create another square struct with one field depicting the side of square of type float and embed the Rectangle struct in it i.e. all the fields and methods of the Rectangle will accessed inside square

  • Step 5 − In the main function, set the width and height of the rectangle using the dot notation and call the promoted area method

  • Step 6 − Finally, the area of the rectangle will be printed on the console using Println function from fmt package where ln means new line

Example 1

In this example, we will create two structs Rectangle and Square in which we will embed the Rectangle struct in the Square struct and all the fields and methods of the Rectangle can be accessed by the Square.

package main

import (
   "fmt"
)

type Rectangle struct {
   width, height float64  
}

func (rect Rectangle) Area() float64 {
   return rect.width * rect.height     
}

type Square struct {
   side float64     
   Rectangle
}

func main() {
   sqr := Square{side: 6}
   sqr.width = 3
   sqr.height = 4    
   fmt.Println("The area of rectangle is:")
   fmt.Println(sqr.Area()) 
}

Output

The area of rectangle is:
12

Example 2

In this example, Vehicle and Car struct will be used to show promoted methods. Here, the Vehicle will be embedded inside the car which means that method and fields under Vehicle are now accessible by the Car struct.

package main

import (
   "fmt"
)

type Vehicle struct {
   color string         
}

func (vch *Vehicle) Start() {
   fmt.Println("Starting vehicle...") 
}

type Car struct {
   brand string
   *Vehicle      
}  

func main() {
   cr := Car{
      brand: "Tatan Motors",
      Vehicle: &Vehicle{
         color: "Grey",
      },
   }
   fmt.Println(cr.color) 
   cr.Start()          
}

Output

Grey
Starting vehicle...

Example 3

In this example, we will write a Golang program to show use of promoted methods using the Person and Employee struct where greet will be the promoted method as it can access the Person struct.

package main

import "fmt"

type Person struct {
   name string
   age  int
}

func (p Person) greet() {
   fmt.Println("Hello, my name is", p.name)
}

type Employee struct {
   Person
   Salary float64
}

func main() {
   emp := Employee{Person{"Kanika", 26}, 150000.00}
   emp.greet()
}

Output

Hello, my name is Kanika

Conclusion

We compiled and executed the program of showing promoted methods using three examples. In the first example, we used Rectangle and Square struct to use promoted methods, in the second example, we used Vehicle and Car struct and in the third example we used Person and Employee struct to execute the program. Hence, the program executed successfully.

Updated on: 04-Apr-2023

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements