Golang Program to Implement the Structs Method


February 8, 2022, Learn eTutorial
1188

 

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

In this section, we’ll focus our attention on the structs method. Before that now you can understand what struct is. A struct in Golang is a user-defined type that contains a collection of named fields/properties. That means it combines items of possibly different types into a single type or single unit. By using the struct keyword, you can define a structure on Go.

Example:

type Employee struct {                                                           
    id int                                                                      
    name string    
               post string                                                             
    salary  int     
}

Here the type keyword is used to introduce a new type. It's followed by the name of the type Employee. The keyword struct is used to indicate that we are defining a struct type and list the fields of struct such as id, name, post, and salary inside of curly braces. Here each field has a name and a type. 

Go Struct Methods

In Go programming, structs can also define methods by using struct objects. When a Method is attached to structs, It works like a normal function. But it additionally needs to specify its type.


type type_name struct { }
func (m type_name) function_name() int {
    //code
}
 

How to implement the structs method in Go 

In this section, we’ll focus our attention on structs. For Example, an employee record has an id, name, post, Salary of one day, and leave. It makes sense to group these five properties into a single structure Employee. By using the structure concepts you can read and display employee records.

The given below program will explain how to create a struct in Go programming, creating an instance, setting values to struct instance, reading and printing struct instances.

Here we can use the built-in function fmt.scanln() to read the input value. Output can display by using the built-in function fmt.println(). Both built-in functions are defined under the fmt package. In order to use these functions, we should import the “fmt” package into our program.

Given below are the steps that are used in the Go program to implement Structs Method.

ALGORITHM 

STEP 1: Start
STEP 2: Import the package fmt
STEP 3: Define a structure 'Employee' which includes id, name, post, Salary of one day, and leave of the employee.
STEP 4: Open the main() to start the program, GO program execution starts with the main()
STEP 5: Declare structure variables emp1 
STEP 6: Read the employee details by using emp1 
STEP 7: Display the Employee details by using the struct method show().
STEP 8: Calculate the Employee working days into w by calling struct method totalWorkingDays()
STEP 9: Display the Employee working days
STEP 10: Calculate and display employee’s salary of the month by calling struct method calculateSalary(w)
STEP 11: Exit

Steps to implement struct method show().

STEP 1: Displays student details with the help of struct object e

Steps to implement struct method totalWorkingDays()

STEP 1: Return the value of 30 - e.leave

Steps to implement struct method calculateSalary(workingDays)

STEP 1: Return the value of workingDays  X e.daySalary

Golang Source Code

                                          package main                                                                 
                                                                             
import (                                                                     
 "fmt"                                                                     
)                                                                            
                                                                             
//define a struct 'Employee'                                                  
type Employee struct {                                                        
 id int                                                                   
 name string    
    post string  
    daySalary int                                                        
 leave  int
                                                                                       
}                                                                            
   func (e Employee) show() {
 // displays student details  
fmt.Println("Employee Details")                                            
 fmt.Println("----------------")                                          
 fmt.Printf("Id: %d\n", e.id)                                      
 fmt.Printf("Name: %s\n", e.name)     
     fmt.Printf("post: %s\n", e.post)
     fmt.Printf("Salary of one day: %d\n", e.daySalary)                 
 fmt.Printf("leave: %d\n", e.leave)                                
 
}

// Methods to calculate total working days using struct instance
func (e Employee) totalWorkingDays() int {
 return (30-e.leave)
}
 
// Struct Methods to calculate total Salary of month
func (e Employee)calculateSalary(workingDays int) int {
 return (workingDays * e.daySalary)
}
                                                                          
func main() {                                                                
 var emp1 Employee                                                     
                                                                             
 // setting values                                                        
 emp1.id = 101                                                          
 emp1.name ="Employee 1"
 emp1.post ="Admin"
 emp1.daySalary=500
  emp1.leave=5                              
                                                                             
   
 // displays Employee details 
    emp1.show()

    // displays Employee Working Days
    var w int
    w = emp1.totalWorkingDays()
 fmt.Printf("Total Working Days: %d\n", w)

 // displays Employee Salary
    fmt.Printf("Total Salary of month: %d\n", emp1. calculateSalary(w))
               
} 
                                      

OUTPUT

Employee Details
----------------
Id: 101
Name: Employee 1
post: Admin
Salary of one day: 500
leave: 5
Total Working Days: 25
Total Salary of month: 12500