Structs, Attaching Methods
In Go, a struct is a composite data type that groups together variables (fields) under a single name. Each field can have a different type, making structs a powerful way to represent complex data structures.
- Define a struct
type rect struct { width int32 height int32}
- Creating a new instance of the rect struct
r := rect{width: 200, height: 100}
- Writing a function to calculate the area of a rect
func area(r rect) int32 { return r.height * r.width}
- Print the area
fmt.Println(area(r))