index
A
alloc_objects type 341
alloc_space type 341
allocations, reducing 331 – 334
API changes 331
compiler optimizations 332
sync.Pool 332 – 334
any type 28 – 32
map of any 243 – 244
APIs 6, 331
append function 65, 77
automaxprocs library 354
B
benchmarks 285 – 294
being fooled by observer effect 291 – 294
-benchtime flag 285 – 294
-count flag 288
making wrong assumptions about micro-benchmarks 287 – 289
not being careful about compiler optimizations 289 – 291
not resetting or pausing the timer 286 – 287
benchstat tool 288
block offset 309
block profiling 343
branching hazard 317
break statement 108 – 110
bugs 4 – 5
build tags 263 – 264
builder pattern 42 – 44
byte slice 332 – 334
byte type 322
bytes package 20, 121, 123
bytes.Compare function 93
C
cache line 302
cache placement policy 307
caches, CPU. See CPU caches
cancel function 187 – 188, 190
categorizing testing 262 – 266
build tags 263 – 264
environment variables 264 – 265
short mode 265 – 266
CFS (Completely Fair Scheduler) 352
chan struct{} 205
channel size 211 – 213
channels 91, 173 – 174, 200 – 204
nil channels 205 – 210
Clean Code (Martin) 5
Close method 38, 158, 247, 252 – 253, 255
close(ch) statement 207
Closer interface 158
closures 45
code documentation, missing 51 – 53
code points 114
common shared package 48
common utility package 49
compilation 2
compiler optimizations 332
Completely Fair Scheduler (CFS) 352
complexity 5
compulsory misses 302
concurrency
channel size 211 – 213
channels or mutexes, when to use 173 – 174
concurrency impacts of workload type 181 – 186
context type 186 – 192
cancellation signals 187 – 188
catching context cancellation 190 – 192
context values 188 – 190
deadline 186 – 187
data races, creating with append 216 – 218
errgroup 228 – 231
goroutines
not being careful with loop variables and 198 – 200
starting without knowing when to stop it 196 – 198
mixing up with parallelism 163 – 166
mutexes, using inaccurately with slices and maps 218 – 221
nil channels 205 – 210
notification channels 204 – 205
parallelism enabled by 165
propagating inappropriate context 193 – 196
race problems 174 – 181
data races vs. race conditions 174 – 179
Go memory model 179 – 181
select and channels, expecting deterministic behavior using 200 – 204
string formatting, side effects with 213 – 216
deadlock 214 – 216
etcd data race 213 – 214
sync type, copying 231 – 233
sync.Cond 223 – 228
sync.WaitGroup 221 – 223
thinking concurrency is always faster 166 – 172
parallel merge sort 169 – 172
scheduling 166 – 169
concurrent mark-and-sweep 348
concurrent threads 166
conflict misses 311
constraints 32
consumer side interface 25
context switching 166
context type 186 – 192
cancellation signals 187 – 188
catching context cancellation 190 – 192
context values 188 – 190
deadline 186 – 187
context.Canceled error 190
context.Context interface 195
context.Context type 186, 190
context.DeadlineExceeded error 190
context.WithTimeout function 187
control hazards 317
control structures
break statement 108 – 110
defer, using inside loops 110 – 112
map iterations 105 – 108
Map insert during iteration 107 – 108
ordering 105 – 107
range loops
arguments evaluated in 98 – 102
elements copied in 95 – 98
pointer elements in 102 – 105
copy function 51, 76 – 77
-coverpkg flag 294
-coverprofile flag 294
CPU caches 300 – 312
cache line 301 – 304
cache placement policy 307 – 312
CPU architecture 300 – 301
predictability 305 – 307
slice of structs vs. struct of slices 304 – 305
CPU profiling 338 – 340
CPU-bound workload 181, 184, 186
cpu.cfs_period_us parameter 352
cpu.cfs_quota_us parameter 352
-cpuprofile flag 339
critical stride 311
D
data alignment 321 – 324
data hazards 317
data races, creating with append 216 – 218
data structures 34
data types
floating-points 61 – 64
integer overflows 57 – 61
concepts 58 – 59
detecting during addition 60
detecting during multiplication 60 – 61
detecting when incrementing 59 – 60
map
initialization 84 – 87
memory leaks and 87 – 89
octal literals 56 – 57
slices
copies of 76 – 77
empty 74 – 76
initialization of 68 – 71
length and capacity of 64 – 68
memory leaks and 80 – 84
slice append 77 – 79
vs. nil 71 – 74
values 90 – 93
database/sql package 30, 145, 244
Deadline method 195
deadlock 214 – 216
defer arguments and receivers 138 – 142
argument evaluation 138 – 141
pointer and value receivers 141 – 142
defer calls 132, 158 – 160
defer functions 111, 139, 144, 157, 187, 250, 252 – 253
defer keyword 110
defer statements 110, 138, 140, 158, 250
errors in 158 – 160
using inside loops 110 – 112
diagnostics tooling 337 – 347
execution tracer 344 – 347
profiling 337 – 344
block profiling 343
CPU profiling 338 – 340
enabling pprof 338
goroutines profiling 342 – 343
heap profiling 340 – 342
mutex profiling 343 – 344
Done method 190, 195
E
Effective Go 131
embedded struct fields 36
empty slices
not properly checking for 74 – 76
vs. nil 71 – 74
encoding package 26 – 27
encoding/binary subpackage 26
encoding/json subpackage 26, 30, 74, 107, 238
environment variables 264 – 265
Equal method 242
Err method 132, 190, 195
errgroup 228 – 231
package 228 – 229
error interface 134 – 135, 153
error management
checking error type inaccurately 149 – 152
checking error value inaccurately 152 – 154
handling defer errors 158 – 160
handling error 156 – 157
handling error twice 154 – 156
panicking 143 – 145
wrapping errors 146 – 149
error type 28, 143, 145
errors.As function 152
errors.Is 153 – 154
escape analysis 329 – 330
etcd data race 213 – 214
Eventually function 277
execution tracer 344 – 347
expected errors 152 – 153
exponents 61
expressivity 2
F
filename as function input 136 – 138
flaky tests 274
float32 type 61
float64 type 61, 128, 244
floating-points 61 – 64
fmt.Stringer interface 214
for loops 97 – 99, 108 – 109, 190, 207, 286
for rows.Next() {} loop 249
forwarding 317
FPU (floating-point unit) 62
fully associative cache 308
functional options pattern 40 – 46
functions and methods
defer arguments and receivers 138 – 142
argument evaluation 138 – 141
pointer and value receivers 141 – 142
filename as function input 136 – 138
named result parameters 129 – 133
nil receiver, returning 133 – 136
receivers, types to use 127 – 129
G
GC (garbage collector) 66, 347
generics, confusion about when to use 31 – 36
common uses and misuses 34 – 36
concepts 31 – 34
getters 17 – 18
global variable 290
go command 268
Go memory model 179 – 181
Go Time podcast 11
GODEBUG environment variable 348
GOGC environment variable 348
golang.org/x repository 288
GOMAXPROCS variable 184, 352
goroutines 179 – 181
channels vs. mutexes, deciding between 173 – 174
deadlines and 186 – 187
deadlock and 215
errgroup 229 – 231
execution tracer and 344 – 347
false sharing and 312 – 315
flaky tests and 274 – 277
Go scheduling and 168
in parallel merge sort 170 – 172
memory heaps and 327 – 328
nil channels and 205 – 207
not being careful with loop variables and 198 – 200
notification channels and 204 – 205
panics 215, 221, 232, 256
profiling 342 – 343
race problems and 174 – 178, 217 – 219, 267
starting without knowing when to stop it 196 – 198
synchronization and 211
worker-pooling pattern and 182 – 186
goto statement 109
Grow method 120 – 121
H
hazards 317
heap
profiling 340 – 342
vs. stack 324 – 330
Heap profile 337
HTTP body 250 – 252
HTTP client 256 – 258
http package 256
http.Client struct 256
http.Client.Timeout 257
http.Get function 257
http.ListenAndServe 259
http.Serve 259
http.Server.ReadHeaderTimeout 259
http.Server.ReadTimeout 259
http.TimeoutHandler timeout 259
http.TimeoutHandler wrapper function 260
http.Transport.ResponseHeaderTimeout timeout 257
http.Transport.TLSHandshakeTimeout timeout 257
HTTP server 259 – 260
httptest package 262, 281 – 283
I
I/O-bound workload 181
if/else statements 9, 11, 318
ILP (instruction-level parallelism) 316
InDelta function 62
init functions 12, 15 – 17, 145
inlining 290, 334 – 337
inlining budget 335
instantiation 32
instruction-level parallelism 315 – 321
instrumentation 266
int types 32 – 33, 58 – 59, 65, 128, 334
int64 types 235, 311, 323
integer overflows 57 – 61
concepts 58 – 59
detecting an integer overflow during multiplication 60 – 61
detecting integer overflow when incrementing 59 – 60
detecting integer overflows during addition 60
-integration flag 264
interface{} 28
interfaces
concepts 18 – 20
interface pollution 24
on producer side 25 – 27
returning 27 – 28
when to use 21 – 24
common behavior 21 – 22
decoupling 22
restricting behavior 23 – 24
inuse_objects type 341
inuse_space type 341
io package 18, 28
io.Closer interface 249, 252 – 253
io.EOF error 153
io.LimitedReader exported struct 28
io.Reader abstraction 19 – 20, 137
io.Reader input 182
io.Reader interface 19, 28, 331
io.ReadFull function 131
io.StringWriter interface 120
io.WriteCloser interface 38
io.Writer abstraction 19 – 20
io.Writer implementation 251
iotest package 262, 283 – 285
J
JSON-handling mistakes 238 – 244
JSON and monotonic clock 240 – 243
map of any 243 – 244
unexpected behavior due to type embedding 238 – 240
json.Marshaler interface 239 – 240
L
len function 99, 115, 121
linters 53 – 54
load factor 86
locality of reference 301
Lock method 37, 336 – 337
loop label 109
loops, using defer inside 110 – 112
M
main function 13 – 14, 325 – 326
main package 12 – 14
make function 86, 211
mantissas 61
maps
initialization 84 – 87
iterations 105 – 108
map insert during 107 – 108
ordering 105 – 107
memory leaks and 87 – 89
Marshal function 30
Marshaler interface 239
MarshalJSON function 239
MarshalJSON method 240
Martin, Robert C. 5
math package 59
math.MinInt integer 60
mechanical sympathy 300
memory fence 222
memory leaks
maps and 87 – 89
slices and 80 – 84
leaking capacity 80 – 81
slice and pointers 81 – 84
memory-bound workload 181
MESI (Modified, Exclusive, Shared, or Invalid) 314
methods. See functions and methods
Mutex profile 338
mutexes 173 – 174, 176
mutual exclusion 176
profiling 343 – 344
using inaccurately with slices and maps 218 – 221
N
named result parameters 129 – 133
NaN (Not-a-Number) 62
nanoseconds 235
nested code 9 – 12
net package 47
net.Dialer.Timeout 257
Network Time Protocol (NTP) 240
nil error 132
nil receiver, returning 133 – 136
nil vs. empty slices 71 – 74
non-unit stride 307
Not-a-Number (NaN) 62
notification channels 204 – 205
now dependency 280
NTP (Network Time Protocol) 240
O
octal literals 56 – 57
off-die 301
on-die 300
optimizations
allocations, reducing 331 – 334
API changes 331
compiler optimizations 332
sync.Pool 332 – 334
concurrent code that leads to false sharing 312 – 315
CPU caches 300 – 312
cache line 301 – 304
cache placement policy 307 – 312
CPU architecture 300 – 301
predictability 305 – 307
slice of structs vs. struct of slices 304 – 305
data alignment 321 – 324
GC (garbage collector) 347 – 352
concepts 347 – 349
examples 349 – 352
Go diagnostics tooling 337 – 347
execution tracer 344 – 347
profiling 337 – 344
inlining 334 – 337
instruction-level parallelism 315 – 321
running Go in Docker and Kubernetes 352 – 354
stack vs. heap 324 – 330
organization of code and project
code documentation 51 – 53
getters and setters 17 – 18
init functions 12 – 17
interfaces
interface pollution 18 – 24
on producer side 25 – 27
returning 27 – 28
nested code 9 – 12
package name collisions 50 – 51
project misorganization 46 – 48
package organization 47 – 48
project structure 46 – 47
suboptimal or unidiomatic 5
suboptimal organization of code 5
utility packages 48 – 50
variable shadowing 7 – 9
os.File 253 – 255
P
package name collisions 50 – 51
package organization 47 – 48
padding 314
-parallel flag 268 – 269
parallel merge sort 169 – 172
parallel threads 166
parallelism 163 – 166, 315 – 321
perflock tool 288
periodic scavengers 348
pointers 81 – 84, 91, 102 – 105, 141 – 142
pprof endpoint 338
printAlloc function 82
println built-in function 325
Producer side interface 25
productivity, lack of 6
profiling 337 – 344
blocks 343
CPUs 338 – 340
enabling pprof 338
goroutines 342 – 343
heaps 340 – 342
mutexes 343 – 344
R
-race flag 174, 231, 266 – 268
race problems 174 – 181
data races vs. race conditions 174 – 179
range expression 100
range loops 31, 95 – 98, 100, 106, 117 – 118
arguments evaluated in 98 – 102
array 100 – 102
channels 100
elements copied in
concepts 96
value copy 96 – 98
pointer elements in 102 – 105
range operator 99 – 101, 103, 116 – 117
Read method 19, 331
readability 5
receivers 126 – 129
redis package 50
reflect package 91
reflect.DeepEqual function 91
reflection 91
regexp package 145
Reset method 237
ResetTimer method 286
return statement 135, 138, 159, 234, 255 – 256, 325, 329
-run flag 273
runes 113 – 115, 123
runnable state 166
runtime.GOMAXPROCS(int) function 184
runtime/trace package 346 – 347
S
Scan error 160
scheduling 166 – 169
select statement 109 – 110, 200 – 204, 207, 209 – 210
set index 309
set-associative cache option 308
SetConnMaxIdleTime parameter 245
SetConnMaxLifetime parameter 245
SetMaxIdleConns parameter 245
SetMaxOpenConns parameter 245
setters 17 – 18
sharing down 329
sharing up 329
-short flag 265 – 266
-shuffle flag 268, 270
SIGPROF signal 338
simplicity of Go 3
slice capacity 68
slice length 68
slices
copies of 76 – 77
empty slices
not properly checking for 74 – 76
vs. nil 71 – 74
initialization of 68 – 71
length and capacity of 64 – 68
memory leaks and 80 – 84
leaking capacity 80 – 81
slice and pointers 81 – 84
slice append 77 – 79
sort package 21, 34
sort.Float64s function 35
sort.Interface interface 34
sort.Ints function 35
sort.Sort(sort.Interface) function 35
SQL mistakes 244 – 249
forgetting about connections pooling 245 – 246
forgetting sql.Open doesn't necessarily establish connections to database 244 – 245
mishandling null values 247 – 248
not handling row iteration errors 248 – 249
not using prepared statements 246 – 247
sql.ErrNoRows sentinel error 153
sql.NullBool wrapper 248
sql.NullFloat64 248
sql.NullInt32 wrapper 248
sql.NullInt64 248
sql.NullString wrapper 248
sql.NullTime 248
sql.NullXXX types 248
sql.Rows 252 – 253
stability 2
stack vs. heap 324 – 330
standard library
HTTP client 256 – 258
HTTP server 259 – 260
JSON-handling mistakes 238 – 244
JSON and monotonic clock 240 – 243
map of any 243 – 244
unexpected behavior due to type embedding 238 – 240
providing a wrong time duration 235
return statement after replying to HTTP request 255 – 256
SQL mistakes 244 – 249
forgetting about connections pooling 245 – 246
forgetting sql.Open doesn't necessarily establish connections to database 244 – 245
mishandling null values 247 – 248
not handling row iteration errors 248 – 249
not using prepared statements 246 – 247
time.After and memory leaks 235 – 238
transient resources 249 – 255
HTTP body 250 – 252
os.File 253 – 255
sql.Rows 252 – 253
Stop() method 237
String method 33, 215 – 216
Stringer interface 214
strings
concatenation 119 – 121
conversions 121 – 123
formatting, side effects with 213 – 216
deadlock 214 – 216
etcd data race 213 – 214
iteration 115 – 118
package 20, 118, 120 – 123
runes 114 – 115
substrings and memory leaks 123 – 125
trim functions 118 – 119
type 32 – 33, 128
strings.Builder struct 120 – 121
struct{} type 48
structural hazards 318
sumValue function 325
superscalar processors 316
switch statement 109 – 110, 201
sync element 233
sync package 128, 176, 223, 231, 233
sync type, copying 231 – 233
sync.Cond package 223 – 228
sync.Mutex type 37, 336
sync.Pool 332 – 334
sync.WaitGroup 221 – 223
Sync() method 254
sync/atomic package 176 – 177, 221
T
task functions 182
testify library 62
testing
categorizing 262 – 266
build tags 263 – 264
environment variables 264 – 265
short mode 265 – 266
enabling race flag 266 – 268
exploring all Go testing features 294 – 297
code coverage 294
setup and teardown 296 – 297
testing from different package 295
utility functions 295 – 296
sleeping in unit tests 274 – 277
table-driven tests 271 – 274
test execution modes 268 – 270
-parallell flag 269 – 270
-shuffle flag 270
time API 278 – 281
utility packages 281 – 285
httptest package 281 – 283
iotest package 283 – 285
writing inaccurate benchmarks 285 – 294
being fooled by observer effect 291 – 294
making wrong assumptions about micro-benchmarks 287 – 289
not being careful about compiler optimizations 289 – 291
not resetting or pausing the timer 286 – 287
threadcreate profile 344
threads 166, 300
time API 281
time package 235
time.After and memory leaks 235 – 238
time.After resources 236
time.Duration API 235
time.Now function 279
time.Time methods 238
time.Time type 240
time.Timer struct 17, 237
-trace flag 344
transient resources 249 – 255
HTTP body 250 – 252
os.File 253 – 255
sql.Rows 252 – 253
trim functions 118 – 119
TrimRight function 118
TrimSpace function 123
TrimSuffix function 118
Truncate method 242
type embedding 36 – 39
U
uint type 58 – 59
under-optimized code 6
unexpected errors 153
unicode/utf8 package 116
unidiomatic organization of code 5
Unit stride 306
Unlock method 37
UUIDs (universally unique identifiers) 123
V
variable shadowing 7 – 9
victim caches 303
virtual cores 300
W
Wait method 170, 221, 226
WithContext function 229
work-stealing 168
worker pooling pattern 182
wrapping errors 146 – 149
Write method 35, 38
WriteHeader method 145
WriteString method 120