Struct Methods
You can define methods on structs, allowing you to associate behavior with your data types. To define a method for a struct, you specify a receiver (which can be a value
or a pointer
) in the method definition:
- Value receiver
type rect struct { width int32 height int32}
func (r rect) area() int { return r.width * r.height}
- Pointer receiver
type rect struct { width int32 height int32}
func (r *rect) area() int32 { return r.height * r.width}