Praise for Know Go....10
Introduction....11
About the book....11
About generics....11
Who is the book for?....12
What should I read first?....13
What will I learn from this book?....13
Completing the challenges....14
How do I install the Go tools?....14
Where are the code examples?....14
1. Interfaces....15
Programming with types....15
Specific programming....15
Generic programming....16
Interface types....16
Interface parameters....17
Polymorphism....19
Interfaces make code flexible....19
Constraining parameters with interfaces....19
Limitations of method sets....20
The empty interface: any....22
Type assertions and switches....23
Go, meet generics....24
How it started....24
How its going....24
2. Type parameters....26
Generic functions....26
Introducing T, the arbitrary type....26
Type parameters....27
Instantiation....27
Stencilling....29
Getting started....29
An Identity function....29
Instantiating Identity....30
Exercise: Hello, generics....31
Running the test....32
Composite types....33
Slices of some arbitrary type....34
Other generic composite types....34
Generic types....35
Defining a generic slice type....35
The elements all have the same type....36
Generic types need to be instantiated....36
Exercise: Group therapy....37
Generic function types....38
Generic functions as values....38
The type is always instantiated....39
There are no generic functions....40
Generic types as function parameters....41
Exercise: Lengthy proceedings....41
Constraining type parameters....42
We cant add any to any....43
Not every type is addable....43
3. Constraints....45
Method set constraints....45
Limitations of the any constraint....45
Basic interfaces....46
Exercise: Stringy beans....47
Type set constraints....49
Type elements....49
Using a type set constraint....49
Unions....50
The set of all allowed types....51
Intersections....52
Empty type sets....52
Composite type literals....53
A struct type literal....53
Access to struct fields....54
Some limitations of type sets....54
Constraints versus basic interfaces....54
Constraints are not classes....55
Approximations....56
Limitations of named types....57
Type approximations....57
Derived types....58
Exercise: A first approximation....59
Interface literals....61
Syntax of an interface literal....61
Omitting the interface keyword....62
Referring to type parameters....63
Exercise: Greater love....65
4. Operations....68
Arithmetic....68
An AddAnything function....69
The set of all numeric types....69
Exercise: Product placement....70
Ordered types....73
The > operator....73
Strings....75
An Ordered interface....75
The cmp.Ordered constraint....76
Multiple type parameters....77
Function on two (or more) types....77
Each type parameter is a distinct type....78
Functions on slice types....78
The problem with derived slice types....79
Parameterizing by slice and element type....81
And I need to know this because?....82
Comparable types....82
Not every type is comparable....83
Not every comparable type is ordered....83
There are infinitely many comparable types....84
The comparable constraint....84
Why is comparable predeclared?....85
Exercise: Duplicate keys....85
Abstract types....88
A Greatest function....88
The scope of type parameters....89
The zero value....90
Switching on abstract types....91
5. Types....94
Named types....94
Named basic types....95
Generic basic types....95
Generic type aliases....96
Generic slice types....96
There are no generic types....97
Slices of interface types....98
Generic map types....99
Maps of abstract element types....99
Multiple type parameters....100
Instantiating multiple type parameters....101
Generic struct types....101
Self-reference....102
Methods....103
Adding methods to generic types....103
Exercise: Empty promises....103
Parameterised methods....105
More generic composite types....105
Interfaces....106
Channels....106
6. Functions....108
Functions on container types....108
Contains....109
Reverse....110
Sort....111
First-class functions....112
Map....112
Type inference....114
Exercise: Func to funky....114
Filtering and reduction....117
Filter....117
Filter functions....118
Generic filter functions....119
Reduce....120
Implementing Reduce....121
Other reduction operations....122
Other considerations....123
Constraints....123
Concurrency....124
Exercise: Compose yourself....125
7. Containers....129
Sets....129
Maps as sets....130
Operations on sets....130
Designing the Set type....131
Building out the machinery....132
The Add method....132
The Contains method....132
Initialising with multiple values....133
Getting a sets members....134
A String method....135
Logic on sets....136
Union....136
Intersection....137
A real-life example....138
Exercise: Stack overflow....138
8. Concurrency....143
Data races....143
Mutexes....144
Deadlocks....144
A concurrency-safe set type....145
Locking....145
The constructor....146
A locking Add method....146
The read-locking methods....147
Testing concurrency safety....148
Smoke tests....148
A fatal error....149
The race detector....150
And the rest....151
Exercise: Channelling frustration....153
Extending the Set type....159
More set operations....159
Key-value stores and caches....160
Other container types....160
9. Packages....162
The cmp package....162
The slices package....163
Comparing slices....163
Finding elements....166
Maxima and minima....168
Inserting and deleting....168
Cloning and compacting....169
Growing and shrinking....170
Sorting....171
Reversing and replacing....172
Searching....173
The maps package....175
Comparing maps....175
Deleting map entries....175
Cloning and copying....176
New idioms....177
Copying slices....177
Deleting slice elements....177
Checking for slice elements....178
Exercise: Merging in turn....179
10. Questions....183
Change....183
How much do I need to know about generics?....183
Will generics drastically change the way I write Go?....184
Do I need to change my existing code?....185
Performance....186
What impact does generics have on performance?....186
Does generic code compile more slowly?....187
Downsides....188
Why didnt they use angle brackets?....188
Still no option types....190
Still no enums....190
No proper union types....191
No macros....191
No parameterised methods....192
No generic packages....192
No insert cool tech here....192
More change....193
Will there be a Go 2?....194
There will be changes to Go....195
It wont be Go 2....195
But there will be a successor to Go (someday)....196
11. Iterators....198
Introduction to iterators....198
Why are iterators useful?....199
The signature of an iterator function....200
Using iterators in programs....201
Single-value iterators: iter.Seq....201
Two-value iterators: iter.Seq2....202
Dealing with errors....204
Cleaning up....205
Embracing iterators....206
Composing iterators....206
When iterators beat channels....207
Standard library changes....208
Slices....208
Maps....209
About this book....211
Who wrote this?....211
Feedback....211
Get my free newsletter....211
Free updates to future editions....212
The Deeper Love of Go....212
The Power of Go: Tools....213
Further reading....213
Credits....214
Acknowledgements....215
Interfaces, generics, and iterators are key to modern Go. Learn all about them with this easy-to-read but comprehensive guide.
If you’re new to Go and generics, and wondering what all the fuss is about, this book is for you! If you have some experience with Go already, but want to learn about the new generics features, this book is also for you. And if you’ve been waiting impatiently for Go to just get generics and iterators already so you can use them, don’t worry: this book is for you too!
You don’t need an advanced degree in computer science or tons of programming experience. Know Go explains what you need to know in plain, ordinary language, with simple examples that will show you what’s new, how the language changes will affect you, and exactly how to use generics in your own programs and packages.
As you’d expect if you enjoyed my previous books, The Deeper Love of Go and The Power of Go: Tools, this is fun and easy reading, but it’s also packed with powerful ideas, concepts, and techniques that you can use in real-world applications. Includes free updates for life.
By reading through this book and completing the exercises, you'll learn:
And more broadly, you’ll get a great understanding of what the introduction of generics and iterators mean for Go as a language and ecosystem, how it affects the standard library and third-party libraries, and how we’ll write programs differently as a result.