Types and OOP in Go
Types and OOP is where you design your own types.
In this chapter, you will:
- point to values with pointers
- group data into structs and give them methods
- build types from parts with composition and embedding
- treat different types the same way with interfaces
- hide data, design custom types, and write generic code
- manage lists and tables of records
What each lesson covers:
- Pointers: Go pointers: memory addresses, the & and * operators, changing values through pointers, nil pointers, pass by value vs pointer parameters, and struct pointers.
- Structs and Methods: Define structs, create and fill them, read, update and compare fields, add methods with value and pointer receivers, write constructors, and use anonymous structs and struct tags.
- Composition and Embedding: Build bigger types from smaller ones: has-a composition, struct embedding with promoted fields and methods, method resolution, and why Go prefers composition over inheritance.
- Interfaces and Polymorphism: Go interfaces: implicit implementation, single and multiple-method interfaces, interface values, nil interfaces, any, type assertions, type switches and polymorphism.
- Encapsulation: Hide data in Go with package-level visibility: export rules, getter methods for read access, read-only patterns, and setter methods with validation.
- Advanced Type Design: Custom types with the type keyword, type aliases, methods on any named type, method sets and their rules, and composing interfaces from smaller ones.
- Generics: Go generics: type parameters, generic functions and return types, generic structs, the any and comparable constraints, custom constraints, and practical generic helpers.
- Collections of Structs: Work with slices and maps of structs: add, update, delete and search records, sort by fields with sort.Slice, filter with custom conditions, and build a CRUD program.
This chapter has 8 lessons. Plan on about 205 minutes, with 27 exercises to check what you learned. Every example is real Go code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.
Simple lesson: finish this chapter and you can model real things, like students, books and accounts, in Go.
Lessons in this section
- Pointers
Go pointers: memory addresses, the & and * operators, changing values through pointers, nil pointers, pass by value vs pointer parameters, and struct pointers.
- Explain pointers and memory addresses
- Use the & and * operators
- Change values through pointers and handle nil
- Use pointer parameters to update original values
- Structs and Methods
Define structs, create and fill them, read, update and compare fields, add methods with value and pointer receivers, write constructors, and use anonymous structs and struct tags.
- Define structs and create instances
- Initialize structs by position and by field name
- Access, update and compare fields
- Write methods with value and pointer receivers
- Composition and Embedding
Build bigger types from smaller ones: has-a composition, struct embedding with promoted fields and methods, method resolution, and why Go prefers composition over inheritance.
- Explain composition and the has-a relationship
- Embed structs and use promoted fields
- Use promoted methods and predict method resolution
- Build complex types from parts
- Interfaces and Polymorphism
Go interfaces: implicit implementation, single and multiple-method interfaces, interface values, nil interfaces, any, type assertions, type switches and polymorphism.
- Explain interfaces and implicit implementation
- Define single and multiple-method interfaces
- Explain interface values and nil interfaces
- Use any, type assertions and type switches
- Encapsulation
Hide data in Go with package-level visibility: export rules, getter methods for read access, read-only patterns, and setter methods with validation.
- Explain encapsulation and data hiding
- Apply Go's export rules at package level
- Write getter methods and read-only patterns
- Write setter methods with validation
- Advanced Type Design
Custom types with the type keyword, type aliases, methods on any named type, method sets and their rules, and composing interfaces from smaller ones.
- Define custom types with the type keyword
- Tell type definitions from type aliases
- Add methods to custom types
- Apply method set rules for values and pointers
- Generics
Go generics: type parameters, generic functions and return types, generic structs, the any and comparable constraints, custom constraints, and practical generic helpers.
- Explain generics, type parameters and type safety
- Write generic functions with generic parameters and results
- Write generic structs with methods
- Use any, comparable and custom constraints
- Collections of Structs
Work with slices and maps of structs: add, update, delete and search records, sort by fields with sort.Slice, filter with custom conditions, and build a CRUD program.
- Make slices and maps of structs
- Add, update, delete and search records
- Sort records by field values
- Filter records with custom conditions