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 HTTP GET request with parameters
Undefined <variable/function> error in Golang
Golang program for implementation of Knuth–Morris–Pratt (KMP) Algorithm
How to check specific field exist in struct?
Program in Go language to Program to Add Two Matrix Using Multi-dimensional Arrays
How to declare Interface Type in Go Programming Language
How do you send an HTTP PUT request in Go?
How to create Slice using new keyword in Golang?
Golang program to read XML file into struct
How to Convert string to integer type in Go?