Accessing Individual fields of a structure
Individual fields of a structure can be accessed easily in Golang using the dot “.” operator. In order to access a particular field, the syntax which is followed is name_of_structure.field_name
Example
package main
import (
"fmt"
)
type Student struct {
firstName string
lastName string
age int
rollNo int
}
func main() {
std1 := Student{
firstName: "John",
lastName: "Rogers",
age: 15,
rollNo: 25,
}
fmt.Println("First Name:", std1.firstName)
fmt.Println("Last Name:", std1.lastName)
fmt.Println("Age:", std1.age)
fmt.Printf("Roll No: %d\n", std1.rollNo)
std1.rollNo = 27
fmt.Printf("New Roll No: %d", std1.rollNo)
}
Output
First Name: John
Last Name: Rogers
Age: 15
Roll No: 25
New Roll No: 27
Zero value of a struct
In Golang, when a struct is defined and if no value is assigned to the different fields explicitly, then the fields of the struct are assigned a value zero (0) by default. In the case of strings, an empty string is assigned to that field.
Example
package main
import (
"fmt"
)
type Student struct {
firstName string
lastName string
age int
}
func main() {
var std1 Student
fmt.Println("First Name:", std1.firstName)
fmt.Println("Last Name:", std1.lastName)
fmt.Println("Age:", std1.age)
}
Output
First Name:
Last Name:
Age: 0
Ways of structure instantiation
There are 4 ways in which an instance can be created :
Using the var keyword
When declaring an instance using the var keyword, we assign values to the struct fields using the name of the structure and the dot operator.
Using a Struct Literal
When declaring an instance using the Struct literal, we assign the values to the fields of the struct at the time of creation.
Using the new keyword
When declaring an instance using the new keyword, we assign values to the struct fields using the name of the structure and the dot.
Example
package main
import "fmt"
type Student struct {
firstName string
lastName string
age int
}
func main() {
// using the var keyword
var std1 Student
std1.firstName = "Jeremy"
std1.lastName = "Shaw"
std1.age = 14
fmt.Println(std1)
// using the struct literal
var std2 = Student{"John", "Roger", 15}
fmt.Println(std2)
std3 := new(Student)
std3.firstName = "Captain"
std3.lastName = "America"
std3.age = 17
fmt.Println(std3)
}
Output
{Jeremy Shaw 14}
{John Roger 15}
&{Captain America 17}
Using pointer address operator
When creating an instance using a pointer address, it is denoted by the & symbol.
Example
package main
import "fmt"
type Student struct {
firstName string
lastName string
age int
}
func main() {
var std1 = &Student{"John", "Rogers", 15} // we cannot skip any value
fmt.Println(std1)
}
Output
&{John Rogers 15}
Nested Struct
Golang allows creating a struct type using another struct type. A structure can be nested by creating a structure type using other structure types as the type for the structure field.
Example
package main
import "fmt"
type Marks struct {
total, cgpa float64
}
type Student struct {
FirstName, LastName string
Age int
marks Marks
}
func main() {
std1 := Student{
FirstName: "John",
LastName: "Rogers",
Age: 15,
marks: Marks{
total: 458.00,
cgpa: 8.10,
},
}
fmt.Println("Full Name :",std1.FirstName, std1.LastName)
fmt.Println("Age :",std1.Age)
fmt.Println("Total Marks:",std1.marks.total)
fmt.Println("CGPA:",std1.marks.cgpa)
}
Output
Full Name : John Rogers
Age : 15
Total Marks: 458
CGPA: 8.1
Add methods to structure
Golang allows the user to add methods to struct types using method receiver.
Example
package main
import "fmt"
type Marks struct {
total, cgpa float64
}
type Student struct {
FirstName, LastName string
Age int
marks Marks
}
func (std Student) info() string {
fmt.Println("Full Name :",std.FirstName, std.LastName)
fmt.Println("Age :",std.Age)
fmt.Println("Total Marks:",std.marks.total)
fmt.Println("CGPA:",std.marks.cgpa)
return "----------------------"
}
func main() {
std1 := Student{
FirstName: "John",
LastName: "Rogers",
Age: 15,
marks: Marks{
total: 458.00,
cgpa: 8.10,
},
}
fmt.Println(std1.info())
}
Output
Full Name : John Rogers
Age : 15
Total Marks: 458
CGPA: 8.1
----------------------
FAQs
-
Can we create a structure without declaring a new data type?
Yes, we can create a structure without declaring a new data type, such structures are called anonymous structs.
-
What is meant by zero value of a struct?
Zero value of a struct means that If we do not explicitly assign values to different fields of a struct, then the fields of the struct are assigned a value zero by default.
-
How can we access individual fields of a structure?
The individual fields of a structure can be accessed using the dot operator. For example, if we want to access the name field of a structure whose instance is named temp, then we can access the name field by “temp.name”.
Key Takeaways
In this article, we have extensively discussed what structures are, how to declare them, the zero value of a struct, and the different ways of structure instantiation with examples in the Go programming language and their implementation in Visual Studio Code.
We hope that this blog has helped you enhance your knowledge regarding structures in the Golang programming language and if you would like to learn more, check out our articles on what golang is, what are the dynamic of Golang, what structures are in C++, and what is the difference between structures and union in C++.
Do upvote our blog to help other ninjas grow. Happy Coding!”