Go Structures
Go Structure is a datatype that allows you to store different properties under a single variable name. It is similar to a class, in object oriented programming, that has only properties. It is very similar to structure in C programming.
In this tutorial, we will learn how to define a structure, declare variables of this structure type and initialize structure variables.
Declare Structure
To declare a structure in Go, use the keywords type and struct.
The syntax to declare a structure is
type struct_name struct {
member definition;
member definition;
...
member definition;
}
The following is an example, to declare student
structure datatype with properties: name
and rollNumber
.
example.go
package main
type student struct {
name string
rollNumber int
}
func main() {
}
Declare Variables of the Structure Datatype
Now we will declare variables of the structure datatype that is declared above.
example.go
package main
type student struct {
name string
rollNumber int
}
func main() {
// declare variables of Student type
var student1 student
var student2 student
}
Initialize Structure Datatype Variables
We can either initialize the structure variable during its declaration or in another statement.
example.go
package main
type student struct {
name string
rollNumber int
}
func main() {
// intialize during declaration
var student1 = student{name:"A", rollNumber:14}
var student2 student
// intialize explicitly
student2 = student{name:"B", rollNumber:13}
}
Access Structure Members
To access members of a structure, use dot .
operator. You can access a specific member by writing structure variable, followed by dot and then the member variable name.
In the following example, we have used dot operator to set the value of member of the structure variable and also to get the value of a member.
example.go
package main
import "fmt"
type student struct {
name string
rollNumber int
}
func main() {
var student1 = student{name:"Anu", rollNumber:14}
// get the member value
fmt.Printf("student1 details\n--------------\n")
fmt.Printf("Name : %s\n", student1.name)
fmt.Printf("Rollnumber : %d\n", student1.rollNumber)
var student2 = student{name:"Arjit", rollNumber:13}
// set the member value
student2.rollNumber=24
student2.name = "Bungi"
fmt.Printf("\nstudent2 details\n--------------\n")
fmt.Printf("Name : %s\n", student2.name)
fmt.Printf("Rollnumber : %d\n", student2.rollNumber)
}
Output
student1 details
--------------
Name : Anu
Rollnumber : 14
student2 details
--------------
Name : Bungi
Rollnumber : 24
Methods on Struct
You can associate behavior as well to structs, in addition to properties, in the form of functions. In the following example, we have a struct defined, and an associated method to the Student struct.
example.go
package main
import "fmt"
// struct definition
type Student struct {
name string
rollNumber int
}
// associate method to Strudent struct type
func (s Student) PrintDetails() {
fmt.Println("Student Details\n---------------")
fmt.Println("Name :", s.name)
fmt.Println("Roll Number :", s.rollNumber)
}
func main() {
var stud1 = Student{name:"Anu", rollNumber:14}
stud1.PrintDetails()
}
Output
Student Details
---------------
Name : Anu
Roll Number : 14
Conclusion
In this Golang Tutorial, we learned what a structure is in Go programming, and how to use it, with the help of examples.