← Go Programming learning pathGo Programming · Module 5 of 10
Structs and methods
Group data with structs and attach behaviour with methods.
📖 Key ideas
- type User struct { Name string; Age int }.
- Create: u := User{Name: "Asha", Age: 21}.
- Methods: func (u User) Greet() string { }.
- Pointer receivers (u *User) can modify the struct.
- Go has no classes or inheritance; use composition (embedding).
💡 Real-life example: Structs are forms with fields; methods are the actions you can do with a filled form.
structCollection of named fields
MethodFunction with a receiver
Pointer receiverMethod that can modify the value
EmbeddingComposition by including a struct