Structs, methods and interfaces
Your own types with structs, behaviour with methods and pointers, and flexible code with interfaces.
About 4 hours of study
Learning objectives
- Model data with structs
- Add methods and use pointers
- Write code against interfaces
Lessons
- 1
Structs
Grouping related values into your own type with struct: declaring, creating with named fields, reading and changing fields, nesting, slices of structs, comparing and printing structs.
- 2
Methods and pointers Intermediate
Adding methods to your types, pointers with & and *, value and pointer receivers, why a method must use a pointer to change its value, constructors and the String method.
- 3
Interfaces Intermediate
Describing behaviour with interfaces: implicit implementation, using an interface as a parameter or slice element, type assertions and type switches, the empty interface, and sort.Interface.
Module practice
- Go: Distance between pointsEasy
- Go: Rank the studentsMedium
- Go: A small inventoryHard
- Go: A counter with methodsEasy
- Go: A bank accountMedium
- Go: A stack made of linked nodesHard
- Go: Areas of shapesEasy
- Go: Who speaks?Medium
- Go: Sort with sort.InterfaceHard
Module quiz
‹ Slices, maps and stringsErrors, goroutines and a complete program ›