← Go Programming learning path
Go Programming · Module 5 of 10

Structs and methods

Group data with structs and attach behaviour with methods.

📖 Key ideas

  1. type User struct { Name string; Age int }.
  2. Create: u := User{Name: "Asha", Age: 21}.
  3. Methods: func (u User) Greet() string { }.
  4. Pointer receivers (u *User) can modify the struct.
  5. 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