In this article, We are going to learn about structure in go golang. The significance and use of structure in go golang with code and examples.
In go golang, Structure is a user-defined data type. It allows you to combine data items of different or same data types. If you know oop (object oriented programming) then you can relate structure with class.
We can also say that a structure is a schema containing the blueprint of data . In layman term, structures are used to represent a record.
To define a structure, we use “struct” keyword.
Syntax:
type structure_name struct {
variable_name_1 variable_name_1_data_type
variable_name_2 variable_name_2_data_type
variable_name_3 variable_name_3_data_type
}
In the above syntax, structure_name is a struct type. variable_name_1, variable_name_2 and variable_name_3 are fields of variable_name_1_data_type, variable_name2_data_type and variable_name_3_data_type respectively.
Example:
Suppose you want to keep track record of students in a school. Then you can define these given below fields in structure:
type Students struct {
Name string
Roll_number int
Address string
Branch string
}
We can also combine variables with same data types in a single line. Example is given below.
type Students struct {
Name, Address, Branch string
Roll_number int
}
How to access structure members:
We use member access operator i.e dot (.) . it comes between structure variable name and structure member name.
How to Initialise structure:
There are many ways to initialise structure. These are given below.
1: Syntax
structure_variable_name := structure_name{
structure_member_name_1: structure_member_value_1,
structure_member_name_2: structure_member_value_2,
structure_member_name_3: structure_member_value_3,
}
Example:
student_1 := Students{
Name: "John",
Address : "Settle",
Branch : "Computer Science",
Roll_number : 123,
}
2: Syntax
var struct_variable_name structure_name
struct_variable_name.structure_member_name_1 = structure_member_1_value
struct_variable_name.structure_member_name_2 = structure_member_2_value
…………………………
struct_variable_name.structure_member_name_n = structure_member_n_value
Example:
// Declare structure variable
var stud Students
// Fill the structure variables
stud.Address = "Settle"
stud.Branch = "Computer Science"
stud.Roll_number = 123
Program to demonstrate of structure in go golang
package main
import (
"fmt"
)
type Students struct {
Name string
Roll_number int
Address string
Branch string
}
func main() {
var student_1 Students /* Declare student_1 of type Students */
var student_2 Students /* Declare student_2 of type Students */
/* Fill the student_1 fields */
student_1.Name = "John"
student_1.Roll_number = 123
student_1.Address = "Settle"
student_1.Branch = "Computer Science"
/* Fill the student_2 fields */
student_2.Name = "Harry"
student_2.Roll_number = 100
student_2.Address = "LA"
student_2.Branch = "IT"
/* print student_1 info */
fmt.Printf( "student_1 Name : %s\n", student_1.Name)
fmt.Printf( "student_1 Roll_number : %d\n", student_1.Roll_number)
fmt.Printf( "student_1 Address : %s\n", student_1.Address)
fmt.Printf( "student_1 Branch : %s\n", student_1.Branch)
/* print student_2 info */
fmt.Printf( "\nstudent_2 Name : %s\n", student_2.Name)
fmt.Printf( "student_2 Roll_number : %d\n", student_2.Roll_number)
fmt.Printf( "student_2 Address : %s\n", student_2.Address)
fmt.Printf( "student_2 Branch : %s\n", student_2.Branch)
}
Output:
student_1 Name : John
student_1 Roll_number : 123
student_1 Address : Settle
student_1 Branch : Computer Science
student_2 Name : Harry
student_2 Roll_number : 100
student_2 Address : LA
student_2 Branch : IT
Pointer to structure:
You can get a pointer to a struct
using the &
operator
How to access member of pointer type structure:
We can access member of pointer type structure with or without using asterisk (*) .
Example:
(*structure_variable_name).structure_member_name
or
structure_variable_name.structure_member_name
Declaration of pointer to structure:
Syntax :
variable_name := &structure_name{struture_variable_name: structure_variable_value, .....}
Or
var structure_variable_name *structure_name
structure_variable_name = &structure_address
Example:
student_1 := & Students{
Name: "John",
Address : "Settle",
Branch : "Computer Science",
Roll_number : 123,
}
or
// Declare student_2 variable as pointer of Students structure
var student_2 *Students
// Create student_1 variable Students structure
student_1 := & Students{
Name: "John",
Address : "Settle",
Branch : "Computer Science",
Roll_number : 123,
}
// Assign address of student_1 structure to student_2.
student_2 = & student_1
Program to demonstrate pointer to structure:
package main
import (
"fmt"
)
type Students struct {
Name string
Roll_number int
Address string
Branch string
}
func main() {
var student_1 Students /* Declare student_1 of type Students structure */
var student_3 *Students /* Declare pointer type variable */
/* Fill the student_1 fields */
student_1.Name = "John"
student_1.Roll_number = 123
student_1.Address = "Settle"
student_1.Branch = "Computer Science"
/* Assigning address of student_1 to student_3 variable */
student_3 = &student_1
fmt.Printf( "student_3 Name : %s\n", student_3.Name)
fmt.Printf( "student_3 Roll_number : %d\n", student_3.Roll_number)
fmt.Printf( "student_3 Address : %s\n", student_3.Address)
fmt.Printf( "student_3 Branch : %s\n", student_3.Branch)
}
Output:
student_3 Name : John
student_3 Roll_number : 123
student_3 Address : Settle
student_3 Branch : Computer Science
Struct meta-data to fields:
We can add meta-data to the fields of structure. This is used to give transformation info on how a structure field is encoded to or decoded from another format like stored or retrieved from a database.
Example:
type Student struct {
firstName string `json:"firstName"`
lastName string `json:"lastName"`
rollNumber int `json: "rollNumber"`
}
In the above structure, we are using struct type as Student for JSON encoding/decoding purpose.
References:
https://www.tutorialspoint.com/go/go_structures
https://medium.com/rungo/structures-in-go-76377cc106a2
To learn more about golang, Please follow given below link .
https://techieindoor.com/go-lang-tutorial/