Nested structure in Golang
A structure define inside structure are known as nested structure. It can be either define as embedded structure or define as separate structure and variable define inside other structure.
Nested structure
 1 package main
 2 
 3 import "fmt"
 4 
 5 type Date struct {
 6 	dd int8
 7 	mm int8
 8 	yy int8
 9 }
 10 
 11 type Employee struct {
 12 	id     int32
 13 	name   string
 14 	salary float32
 15 	doj    Date // instance of structure inside employee
 16 }
 17 
 18 func main() {
 19 	// creating structure array
 20 	var employees = []Employee{
 21 		{id: 1, name: "User 1", salary: 27000, doj: Date{dd: 12, mm: 01, yy: 11}},
 22 		{id: 2, name: "User 2", salary: 22000, doj: Date{dd: 15, mm: 03, yy: 12}},
 23 	}
 24 
 25 	// displaying array of structure
 26 	for _, emp := range employees {
 27 
 28 		fmt.Printf("Id : %d", emp.id)
 29 		fmt.Printf(", Name : %s", emp.name)
 30 		fmt.Printf(", Salary : %.2f", emp.salary)
 31 		fmt.Printf(", DOJ : %.2d-%.2d-%.2d", emp.doj.dd, emp.doj.mm, emp.doj.yy)
 32 		fmt.Println()
 33 	}
 34 }
 35 
In the above example, we have define two structure Date and Employee. A structure date is used inside Employee structure as field. It is a customer data type member which is used in Employee structure.
In the main function, we have define array and initialize array with Employee. An employee instance property doj is initialize using Date structure instance. An array is printed using for loop.
Output
 1 Id : 1, Name : User 1, Salary : 27000.00, DOJ : 12-01-11
 2 Id : 2, Name : User 2, Salary : 22000.00, DOJ : 15-03-12

Example 2

Nested structure
 1 package main
 2 
 3 import (
 4 	"fmt"
 5 	"sort"
 6 )
 7 
 8 type Tyre struct {
 9     company   string
 10     size      string
 11 }
 12 	
 13 type Car struct {
 14 	model   string
 15 	company string
 16 	price   float32
 17 	tyre    Tyre
 18 }
 19 
 20 func main() {
 21 
 22     var cars = []Car{
 23         { model: "Q7", company: "Audi", price: 125487.26, 
 24             tyre: Tyre {company: "MRF", size: "5x" }},
 25         { model: "X5", company: "BMW", price: 562145.365, 
 26             tyre: Tyre {company: "Brighton", size: "5v"}},
 27     }
 28     
 29     // sorting cars based on tyre company in ascending order
 30     sort.Slice(cars, func(i, j int) bool {
 31         // change property sorting need to be change based on other property
 32         return cars[i].tyre.company < cars[j].tyre.company
 33     })
 34     
 35     // displaying array of structure
 36     for _, car := range cars {
 37     
 38         fmt.Printf("%5s    ", car.model)
 39         fmt.Printf("%5s     ", car.company)
 40         fmt.Printf("%.4f   ", car.price)
 41         fmt.Printf("Tyre : (%s, %s)", car.tyre.company, car.tyre.size)
 42         fmt.Println()
 43     }
 44 }
In the above example, a structure object is define with nested structure where Tyre structure object is specified in Car structure. An Array of structure is created by specifying a nested objects. An array of structure is sorted based on Tyre company name in ascending order. A sorted array is printed which will print sorted array based on Tyre company name.
Output
 1    X5      BMW     562145.3750   Tyre : (Brighton, 5v)
 2    Q7     Audi     125487.2578   Tyre : (MRF, 5x)
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us