Creating Instances of Struct Types
The var keyword initializes a variable rect. Using dot notation, values are assigned to the struct fields.
Example
package main
import "fmt"
type rectangle struct {
length int
breadth int
color string
geometry struct {
area int
perimeter int
}
}
func main() {
var rect rectangle // dot notation
rect.length = 10
rect.breadth = 20
rect.color = "Green"
rect.geometry.area = rect.length * rect.breadth
rect.geometry.perimeter = 2 * (rect.length + rect.breadth)
fmt.Println(rect)
fmt.Println("Area:\t", rect.geometry.area)
fmt.Println("Perimeter:", rect.geometry.perimeter)
}
Most Helpful This Week
Golang program to print all Permutations of a given string
Golang program for implementation of Shell Sort
How do you handle HTTP requests in Go?
Cannot convert <type1> to <type2> error in Golang
Contains, ContainsAny, Count and EqualFold string functions in Go Language
How to slice elements in Golang?
How do you handle HTTP server health checks in Go?
How to check specific field exist in struct?
How to Convert Float to String type in Go?
Golang program for implementation of Floyd–Warshall Algorithm