Updated April 7, 2023
Introduction to Golang Struct
In Go language we have seen various data types, here struct is another data type but it is not the system-defined data type like an array and int or string but it is a user-defined data type, it allows developers to the defined mixed data type. In a simple and more clear way, we can say that with the help of the struct type we can define a variable where we can define int, string, and float, etc. type of data type at one place. The most important uses of this type of variable is understandability by other by which the variable, anyone can easily understand the uses and types of data needed for any variable. Let us discuss the Golang Struct below.
Syntax
In the below simple syntax, we have tried to create a simple syntax for the struct. Let me discuss the syntax in a few steps below.
- First, we write the types of the struct variable, here type is the keyword that defines the type of any variables.
- After type, we need to write the defined name of the struct, as this name will be used wherever we want to use this data type and we need to use respective attributes which we have defined inside it.
- Inside we have written some attributes; these are various other mixed variables, where we can define int, string, float instead of the data type of the attribute.
Please see the below syntax:
type struct-type-name struct {
Attribute 1 data type of attribute
Attribute 2 data type of attribute
...
}
How Does Struct Work in Go language?
To understand the working of the struct data type in the go language first we should understand why we need this, suppose in case we wanted to define some details of employee and inside that details you wanted always same name and data type each attribute like name of the employee, salary, and address of the employee. So in such cases, we can not use simple variables as everyone will try to use the different names and types. So once we will define the struct type with fixed name and data type of each attribute whoever will use these attributes they need to use the same name and data types elas it will not work. It’s working we can defined in the few steps.
- It works like any simple class of other languages, or we can say it is a simple light weight class.
- We can use these struct types of data as the composition, but we can not use them as an inheritance.
- Once we define or write type, the compiler expects the name of the variable.
- Once we define the name of the variables we can give the name of the attributes along with data types of all attributes of them.
- Whoever will try to use these struct data types of the variables they can only use those variables which we have defined inside the struct data types.
- For example, if we have an address struct and inside the address, we can define the variables like the name of the city as string data type, Pin code as int variable, etc. And the one who wants to use struct they can initialize the city as the string name and Pin code as the integer value.
Examples to Implement of Golang Struct
Below are the examples of the Golang Struct:
Example #1
In the below example, we have taken a struct of location type where we have defined location name, city, and pin code with different different types of variables. So we have taken all the important attributes of the Location type struct with different data types inside the struct Location.
Please see the below example:
Code:
package main
import "fmt"
// Here we are defining the struct type
type Location struct {
locName string
locCity string
locPicCode int
}
//Creating main function
func main() {
var l Location
fmt.Println(l)
//Here we are declaring the struct and initialising for first
l1 := Location{"Ranjan", "Dhanbad", 828123}
fmt.Println("First location: ", l1)
//Here we are declaring the struct and initialising for second
l2 := Location{locName: "Ranjan", locCity: "Chennai",
locPicCode: 600001}
fmt.Println("Second Location: ", l2)
//Here we are declaring the struct and initialising for third
l3 := Location{locName: "Delhi"}
fmt.Println("Third location: ", l3)
}
Output:
Example #2
Below is a very simple example where we have taken the struct type human, and we know a human can have a name as string, a human can have an address with string as the data type also in human height in feet with the float as the data types. We have taken various types of attributes of a human with each attribute containing different data types.
Please see the below example:
Code:
package main
import "fmt"
type Human struct {
Name, Sex, Address string
Height float64
}
func main() {
//Here we are declaring the struct and initialising for third
h := Human{Name: "Ranjan", Sex: "Male",
Address: "Height", Height: 5.11}
fmt.Println("Human Name is: ", h.Name)
fmt.Println("Human Sex: ", h.Sex)
//Here we are modifying the struct .
h.Sex = "Female"
h.Name= "Ankita"
fmt.Println("Human detail is: ", h)
}
Output:
Example #3
Below is a simple example for a struct to define a staff details, here inside the staff struct we have taken various mixed data types like staff name (string), staff salary (int), etc.
Please see the below example along with the screen of output.
Code:
package main
import "fmt"
// Here we are defining the struct for uses
type Staff struct {
staffFirstName, staffLastName string
staffAge, staffSalary int
}
func main() {
// Initialising and declaring the struct
staff1 := &Staff{"Sam", "Anderson", 55, 6000}
fmt.Println("The first name of the staff is:", (*staff1).staffFirstName)
fmt.Println("Age of the staff is:", (*staff1).staffAge)
}
Output:
Conclusion
From this tutorial we learned the basic concept of the struct of the go language, we learned about the working and syntax of the struct type. We focus on the various examples of the struct which elaborate and revealed the important facts about the struct.
Recommended Article
This is a guide to Golang Struct. Here we discuss the introduction, syntax, and working of struct in go language along with examples and Code Implementation. You can also go through our other suggested articles to learn more –