Golang Program to Implementation of Structs


March 17, 2022, Learn eTutorial
1122

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

What is the  Struct  in Go Programming Language 

A structure or 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. Generally, this is almost the same as the concept of classes in object-oriented programming. You can say a struct works as a lightweight class that supports composition but not inheritance. By using the struct keyword, you can define a structure on Go.


type struct_name struct {
// fields
}

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

 

How to implement the structs  in Go 

In this section, we’ll focus our attention on structs. For Example, an employee record has an id, name, post, and salary. 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.

ALGORITHM 

STEP 1: Start
STEP 2: Import the package fmt
STEP 3: Define a structure 'Employee' which includes id, name, post, and salary 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 and emp2.
STEP 6: Read the employee details by using emp1 and emp2.
STEP 7: Display the Employee details on the screen.
STEP 8: Exit

Golang Source Code

                                          package main                                                                 
                                                                             
import (                                                                     
 "fmt"                                                                    
)                                                                            
                                                                             
//define a struct 'Employee'                                                  
type Employee struct {                                                        
 id int                                                                   
 name string    
               post string                                                          
 salary  int                                                                
}                                                                            
                                                                             
func main() {                                                                
 var emp1 Employee                                                     
 emp2 := new(Employee)                                                 
                                                                             
 // setting values                                                        
 emp1.id = 101                                                          
 emp1.name = "Employee 1"   
  emp1.post = "manager"                                                                                         
 emp1.salary = 60000                                                      
                                                                             
 emp2.id = 102                                                          
 emp2.name = "Employee 2"   
  emp2.post = "Administrator"                                                                                         
 emp2.salary = 30000    
                                    
 // displays Employee details                                              
 fmt.Println("Employees Details")                                           
 fmt.Println("----------------")                                          
 fmt.Printf("Id: %d\n", emp1.id)                                      
 fmt.Printf("Name: %s\n", emp1.name)      
    fmt.Printf("Post: %s\n", emp1.post)  
    fmt.Printf("Salary: %d\n", emp1.salary)                              
   
 fmt.Println("----------------")                                          
 fmt.Printf("Id: %d\n", emp2.id)                                      
 fmt.Printf("Name: %s\n", emp2.name)      
    fmt.Printf("Post: %s\n", emp2. post)  
    fmt.Printf("Salary: %d\n", emp2. salary)                              
}         

                                      

OUTPUT

Employees Details
----------------
Id: 101
Name: Employee 1
Post: manager
Salary: 60000
----------------
Id: 102
Name: Employee 2
Post: Administrator
Salary: 30000