In this tutorial, we will be checking out the Go struct data type which falls under the aggregate data type of composite types. In this tutorial you will gain knowledge of what exactly is a structure in Golang, their definition and declaration syntax in Golang, naming a struct, accessing and nesting struct data types, etc.
A struct is a structure composed of a collection of fields where each field holds different types of data. So, In Golang struct is categorized under aggregate type of composite data type. The fields defined within a struct have different properties & each property is of specified types and of assigned values.
Eg: Consider an Employee database in an organization that keeps records related to all employees such as employee details like name, age, id, place, salary, etc. these are different fields that contain various types of data that refer to a single entity under struct data type.
To define a structure two keywords are required. They are
type emp_details struct{
Name string
Age int
Place string
Id int
Salary int
}
The above code shows a struct named emp_details
with fields
Name, Age, Place, Id, and Salary. The above struct defined is a named struct
because it creates a new struct data type named emp_details.
type
mentions a new type & the keyword struct
defines the type as structure. type
the keyword is followed by the name of the structure (emp_details). string
, Salary is of type int
so on.Each field is written one per line which can be altered by grouping fields with similar data types to a single line as shown below:
type emp_details struct{
Name Place string //single line declaration of same type(string)
Age Id Salary Int //single line declaration of same type(integer)
}
In Go language, a struct is initialized by using var
keyword or shorthand declaration using the symbol “:= ”.Whenever the struct fields are not initialized with any specific values they are set to default type.
Let’s understand one by one using an example
// Golang program to show how to declare and define the struct
package main
import "fmt"
// Defining a struct type
type emp_details struct {
Name string
Age int
Place string
Id int
salary int
}
func main() {
// Declaring a variable e `struct` type using var keyword
var e emp_details
fmt.Println(e)
//Declaring and Initializing variable of struct type
var e1 = emp_details{"miya", 20,"Mumbai",0067,20000}
fmt.Println(e1)
//Declaring and Initializing variable of struct type
var e2 = emp_details{Age: 10, salary: 45000}
fmt.Println(e2)
}
Output:
{ 0 0 0} {miya 20 Mumbai 55 20000} { 10 0 45000}
Explanation
// Golang program to show how to declare and define the struct
package main
import "fmt"
// Defining a struct type
type emp_details struct {
Name string
Age int
Place string
Id int
salary int
}
func main() {
// Shorthand notation Declaration and Initialization
e1 := emp_details{"miya", 20,"Mumbai",0067,20000}
fmt.Println(e1)
//Shorthand notation Declaration and Initialization
e2 := emp_details{Age: 10, salary: 45000}
fmt.Println(e2)
}
Output:
{miya 20 Mumbai 55 20000} { 10 0 45000}
Explanation
// Golang program to show how to declare and define the struct
package main
import "fmt"
// Defining a struct type
type emp_details struct {
Name string
Age int
Place string
Id int
salary int
}
func main() {
// Declaring a variable of a `struct` type
// All the struct fields are initialized with their zero value
var e emp_details
fmt.Println(e)
}
Output:
{ 0 0 0}
Explanation
In Golang Struct fields are accessed by a dot ( .) operator. Getting and setting struct fields are easy is Golang using the dot operator
In a below-given program created a struct emp_details that has 5 fields. To access & assign a value to the field Name of string type, we use syntax
Let’s understand with an example
// Golang program to show how to declare and define the struct
package main
import "fmt"
// Defining a struct type
type emp_details struct {
Name string
Age int
Place string
Id int
salary int
}
func main() {
// Declaring a variable of a `struct` type
// All the struct fields are initialized with their zero value
var e emp_details
//setting values using dot(.) operator
e.Name = "Nainika"
e.Age=5
e.Place = "MALAYSIA"
e.Id = 007
e.salary = 100000
//Displays output
Output:
e.Name : Nainika e.Age : 5 e.Place : MALAYSIA e.Id: 7 e.salary : 100000
In Golang struct data type provides a property called embedding which enables embedding a struct inside another struct. A struct type can be defined as another struct type.
Syntax
type Address struct {
City string
Country string
}
type emp_details struct {
Name string
Age int
Address
}
package main
import "fmt"
type Address struct {
city string
country string
}
type emp_details struct {
name string
age int
Address
}
func main() {
emp := emp_details{
name: "Joseph",
age: 54,
Address: Address{
city: "italy",
country: "USA",
},
}
fmt.Println("Name of employee:", emp.name)
fmt.Println("Age:", emp.age)
fmt.Println("City:", emp.city)
fmt.Println("Country:", emp.country)
}
Output:
Name of employee: Joseph Age: 54 City: italy Country: USA
A pointer to the struct data type is created with the “&” operator. All variables have memory locations and the pointer with & operator allows to access the memory location where the variable operands are allocated whereas the * operator allows accessing its corresponding values held in that location.
Example :
package main
import "fmt"
// declaration of struct type
type emp_details struct {
Age int
Name string
Salary int
}
func main() {
var emp1 = &emp_details{40, "zen", 45000} // all values are shown
fmt.Println(emp1)
var emp2 = &emp_details{}
emp2.Age = 10
emp2.Name = "Vihan"
fmt.Println(emp2) // salary not mentioned
}
Output:
&{40 zen 45000} &{10 Vihan 0}
Explanation
var emp2 = &emp;_details{}
emp2.Age = 10
emp2.Name = "Vihan"