Index

Symbols

A

  • abbreviations in package names, 6

  • accessing struct fields, 172173

  • adding goroutines to waitgroups

  • aliases, resolving name conflicts, 16, 5657

  • All method, 208209

  • anonymous functions, 150, 159160

  • any keyword, 242243

  • APIs, scope, 10

  • append function, 8590, 97

  • appending

  • architecture-independent numeric types, 31

  • arguments, 141142

  • arrays

    • avoiding out-of-bounds errors, 287289

    • converting to slices, 102104

    • creating, 79

    • data types in, 8283

    • indexing, 8182

    • initializing, 7980

    • iteration over, 105

    • purpose of, 77

    • setting values of, 85

    • slices versus, 7779

    • subsets of, 103104

    • type definitions, 8384

    • zero values, 81

  • AsError interface, 303304

  • asserting

    • interface implementation, 241242

    • with type assertions, 250251

  • assigning

    • functions to variables, 283284

    • multiple values to variables, 4546

    • variables, 4142

B

  • binaries, embedding files in, 520521

  • bitwise operators, 135

  • blocking channels, 343344

  • boolean logic

    • boolean operators, 134

    • comparison operators, 135

    • equality comparison, 129130

  • break keyword, 107108

  • buffered channels, 358359

  • byte numeric type, 31

C

  • callback interfaces, defining, 255256

  • calling

  • cancellation deadlines on contexts, 374

  • cancellation events

    • on contexts, 396

    • on error groups, listening for, 439440

  • cap function, 92, 9697, 112113

  • capacity

  • capitalization for exporting variables, 57

  • capturing

    • panic values, 269273

    • system signals, 363

      • implementing graceful shutdown, 365367

      • listening for shutdown confirmation, 368370

      • listening for system signals, 367368

      • os/signals package, 363365

      • timing out nonresponsive shutdown, 370371

  • casting values, 45

  • chan keyword, 343

  • channels

    • blocking/unblocking, 343344

    • buffered, 358359

    • capturing system signals, 363

      • implementing graceful shutdown, 365367

      • listening for shutdown confirmation, 368370

      • listening for system signals, 367368

      • os/signals package, 363365

      • timing out nonresponsive shutdown, 370371

    • characteristics of, 343

    • closing, 352354

      • closing already-closed, 357

      • reading buffered, 362363

      • reading closed, 354357

      • with sync.Once type, 463466

      • writing closed, 358

    • creating, 343344

    • listening with select statements, 348349

    • message queues versus, 349351

    • range loops and, 348

    • sending/receiving values, 344345

    • unidirectional, 351352

    • usage example, 345347

  • choosing numeric types, 32

  • cleaning up test helpers, 227229

  • closing channels, 352354

    • closing already-closed, 357

    • reading buffered, 362363

    • reading closed, 354357

    • with sync.Once type, 463466

    • writing closed, 358

  • closures, 149150

  • code coverage

    • coverage profiles, 203204

    • editor support for, 206

    • go test command, 202203

    • go tool cover command, 204205

    • HTML coverage reports, 205206

  • comparison operators, 135

  • compiled languages, 2830

  • complex values in maps, 123124

  • complex64 numeric type, 31

  • complex128 numeric type, 31

  • concrete types

    • compilation errors for type assertions, 330331

    • interfaces versus, 231233

    • verifying via type assertions, 252

  • concurrency. See also synchronization

    • channels

      • blocking/unblocking, 343344

      • buffered, 358359

      • buffered message delivery, 360362

      • buffered usage example, 359360

      • capturing system signals, 363

      • characteristics of, 343

      • closing, 352354

      • closing already-closed, 357

      • creating, 343344

      • implementing graceful shutdown, 365367

      • listening for shutdown confirmation, 368370

      • listening for system signals, 367368

      • listening with select statements, 348349

      • message queues versus, 349351

      • os/signals package, 363365

      • range loops and, 348

      • reading closed, 354357

      • reading closed buffered, 362363

      • sending/receiving values, 344345

      • timing out nonresponsive shutdown, 370371

      • unidirectional, 351352

      • usage example, 345347

      • writing closed, 358

  • contexts

  • concurrent monitors, starting, 401402

  • const keyword, 48

  • constants, 4851

    • declaring, 48

    • modifying, 48

    • typed, 49

    • untyped, 5051

  • constraints. See type constraints

  • constraints package, 326328

  • constraints.Integer constraint, 327

  • constraints.Ordered constraint, 327328

  • constraints.Signed constraint, 327

  • context package, 373, 378. See also contexts

  • context.Background function, 378380

  • context.Canceled error, 409410

  • context.CancelFunc function, 397398, 402403

  • context.Context interface, 374, 379380

  • context.Context.Deadline method, 374

  • context.Context.Done method, 375376, 398399

  • context.Context.Err method, 376, 408409

  • context.Context.Value method, 376378, 386387

  • context.DeadlineExceeded error, 410411

  • contexts

    • cancellation events, 396

    • deadlines on, 374

    • default implementation, 379380

    • error messages

      • context.Canceled error, 409410

      • context.Context.Err method, 376, 408409

      • context.DeadlineExceeded error, 410411

    • exporting keys, 393394

    • key security, 394396

    • listening for system signals, 411413

    • nodal hierarchy, 381

    • retrieving values, 376378

    • rules for, 380381

    • string keys, 388

    • testing system signals, 413416

    • wrapping with values, 382, 384386

  • context.WithCancel function, 397

  • context.WithDeadline function, 405406

  • context.WithTimeout function, 407408

  • context.WithValue function, 382, 384386

  • continue keyword, 107108

  • control structures

    • if statements

    • logic and math operators, 134135

    • switch statements, 135138

  • converting

    • arrays to slices, 102104

    • strings to/from numbers, 7275

  • copy function, 101102

  • copying

  • coverage profiles, 203204

  • custom data types, 167168

  • custom errors

  • custom string key types, 390393

  • cyclical imports, 2425

D

  • data races

  • data types

    • in arrays, 8283

    • concrete types versus interfaces, 231233

    • constants, 4951

    • declaring new, 167168

    • in function arguments, 141143

    • functions as, 147, 180181

    • generics, 332334

    • inheritance, 181182

    • numeric, 3135

    • architecture-independent, 31

    • choosing, 32

    • implementation-specific, 31

    • overflow versus wraparound, 3234

    • saturation, 3435

  • in slices, 8283

  • static versus dynamic typing, 2728

  • string literals, 3537

    • interpreted, 35

    • quotation marks for, 35

    • raw, 3637

  • third-party, methods on, 178179

  • UTF-8 characters, 3740

    • iterating over, 3840

    • runes, 38

  • deadlines on contexts, 374, 405406, 410411

  • debug.PrintStack function, 307

  • debug.Stack function, 307

  • declaring. See also defining

  • decrementing integers, 122

  • default blocks, 138139

  • default implementations of contexts, 379380

  • defer keyword, 156, 229, 266

  • deferring function calls, 156

    • anonymous functions, 159160

    • exit and fatal messages, 158159

    • with multiple returns, 156157

    • order of execution, 157

    • panics and, 158

    • scope, 160162

    • testing.TB.Cleanup method versus, 229

  • defining. See also declaring

  • delete function, 118119

  • deleting map keys, 118119

  • dependencies, 1622

  • directories. See also files

  • disabling test caching, 221

  • discarding variables, 47

  • do while loops, 108109

  • dog feeding metaphor (concurrency), 336338

  • dynamic typing, 2728

E

  • else if statements, 131132

  • else statements, 130131

  • embed package, 517

  • embedding

  • embed.FS type, 518519

  • empty interfaces, 242243

  • encoding with struct tags, 173176

  • equality comparison, 129130

  • errgroup.Group type, 436, 440442

  • errgroup.Group.Go method, 437

  • errgroup.Group.Wait method, 437439

  • errgroup.WithContext function, 439440

  • error groups

    • errgroup.Group type, 436, 440442

    • errgroup.Group.Go method, 437

    • errgroup.Group.Wait method, 437439

    • errgroup.WithContext function, 439440

    • golang.org/x/sync/errgroup package, 434

    • problem solved by, 434436

  • error interface, 261, 289

  • errors. See also testing

    • arrays versus slices, 103

    • in compilation, 18, 2930, 34

    • context error messages

      • context.Canceled error, 409410

      • context.Context.Err method, 376, 408409

      • context.DeadlineExceeded error, 410411

    • custom

    • errors.As function, 302304

    • errors.Is function, 304306

    • with file path helpers, 488489

    • with formatting verbs, 7071

    • nil receivers, 192193

    • out of bounds, 8182

    • panics

      • assigning functions to variables, 283284

      • avoiding, 273

      • capturing and returning values, 269273

      • checking for nil receivers, 274275

      • checking type assertions, 284287

      • initializing interfaces, 280283

      • initializing maps, 275278

      • initializing pointers, 278280

      • log.Fatal function, 273

      • from out-of-bounds errors, 264265, 287289

      • raising, 265

      • recovering from, 265269

    • returning, 144

    • stack traces, 307308

    • types of, 296

    • unwrapping, 297301

    • as values, 259261

      • error interface, 261

      • errors.New function, 261

      • fmt.Errorf method, 262

      • handling, 262263

      • usage example, 263264

    • in walk tests, fixing, 497500

    • wrapping, 294297

  • errors.As function, 302304

  • errors.Is function, 304306, 489

  • errors.New function, 261

  • errors.Unwrap function, 297301

  • escape characters

    • escaping, 6263

    • for formatting strings, 6162

    • in raw string literals, 36

  • exceptions, 259260

  • exits, deferred function calls and, 158159

  • expanding slices with variadic operator, 153154

  • explicit argument indexes, 7172

  • explicit implementation of interfaces, 233234

  • exporting

  • extensions, retrieving, 485

F

  • failing fast when testing, 220221

  • fallthrough keyword, 139140

  • fatal message, deferred function calls and, 158159

  • file systems

  • file tree, example of, 467468

  • filepath package, 484

  • filepath.Base function, 486

  • filepath.Dir function, 485, 486487

  • filepath.Ext function, 485, 486487

  • filepath.Join function, 505506

  • filepath.WalkDir function, 473474, 475477

  • files. See also directories

  • first-class functions, 147

  • float32 numeric type, 3132

  • float64 numeric type, 3132

  • floats, formatting with formatting verbs, 6667

  • fmt package, functions in, 5758

    • errors with formatting verbs, 7071

    • escape characters, 6163

    • explicit argument indexes, 7172

    • float formatting verbs, 6667

    • formatting verbs, 61

    • Fprint functions, 59

    • integer formatting verbs, 6366

    • multiple Println arguments, 61

    • new lines, 60

    • Print functions, 59

    • Sprint functions, 5859

    • string formatting verbs, 63

    • value Go-syntax printing, 6970

    • value printing, 6769

    • value type printing, 67

  • fmt.Errorf method, 262, 294, 301

  • fmt.Stringer interface, 241

  • folders, package folder structure, 4, 613. See also directories

    • multiple packages, 79

    • naming files, 9

    • organization, 913

  • for loops

    • appending to slices, 8889

    • break keyword, 107108

    • continue keyword, 107108

    • explained, 104105

    • iteration over arrays and slices, 105

    • range keyword, 105106

  • formatting

    • floats with formatting verbs, 6667

    • integers with formatting verbs, 6366

    • strings

      • errors with formatting verbs, 7071

      • escape characters, 6163

      • explicit argument indexes, 7172

      • with formatting verbs, 61, 63

      • list of functions, 5758

      • Sprint functions, 5859

    • verbs for, 61

      • errors with, 7071

      • escape characters, 6163

      • explicit argument indexes, 7172

      • floats, 6667

      • integers, 6366

      • strings, 63

      • value type printing, 67

  • Fprint functions, 59

  • fs package, 506507

  • fs.DirEntry type, 469470

  • fs.ErrExit error, 489

  • fs.File interface, 509

  • fs.FileInfo interface, 471472

  • fs.FS interface, 508, 510513

  • fs.SkipDir error, 478479

  • fstest.MapFile type, 509, 515

  • fstest.MapFS type, 514, 515517

  • fs.WalkDir function, 510512

  • fs.WalkDirFunc function, 474475

  • functions. See also interfaces

    • anonymous, 150

    • arguments, 141142

      • accepting from other functions, 151

      • capturing panic values, 269273

      • error handling, 262263

      • functions as, 148149

      • multiple return arguments, 144145

      • named returns, 145147

      • passing slices with, 153154

      • position of variadic arguments, 152153

      • return arguments, 143144

      • of same type, 142143

      • variadic arguments, 151152

      • when to use variadic arguments, 154156

    • assigning to variables, 283284

    • calling without interfaces, 235237

    • closures, 149150

    • as data types, 180181

    • deferring calls, 156

      • anonymous functions, 159160

      • exit and fatal messages, 158159

      • with multiple returns, 156157

      • order of execution, 157

      • panics and, 158

      • scope, 160162

      • testing.TB.Cleanup method versus, 229

    • definitions, 141

    • as first class, 147

    • in fmt package, 5758

      • errors with formatting verbs, 7071

      • escape characters, 6163

      • explicit argument indexes, 7172

      • float formatting verbs, 6667

      • formatting verbs, 61

      • Fprint functions, 59

      • integer formatting verbs, 6366

      • multiple Println arguments, 61

      • new lines, 60

      • Print functions, 59

      • Sprint functions, 5859

      • string formatting verbs, 63

      • value Go-syntax printing, 6970

      • value printing, 6769

      • value type printing, 67

    • generics, 311

      • calling, 316317

      • constraints package, 326328

      • defining constraints, 321322

      • instantiating, 320321

      • mixing type and method constraints, 331

      • multiple type constraints, 322323

      • multiple types, 317320

      • type assertions, 329331

      • type constraints, 315317

      • underlying type constraints, 324326

    • init, 162163

    • methods on, 181

    • methods versus, 177

    • new, 189190

    • in strconv package, 7275

    • test function signatures, 196

    • test helpers, 222

    • as variables, 147148

G

  • garbage collection, 28

  • generic functions, 311

    • calling, 316317

    • constraints package, 326328

    • defining constraints, 321322

    • instantiating, 320321

    • mixing type and method constraints, 331

    • multiple type constraints, 322323

    • multiple types, 317320

    • type assertions, 329331

    • type constraints, 315317

    • underlying type constraints, 324326

  • generic package names, avoiding, 6

  • generic types, 332334

  • getting pointers, 185186

  • go build command, 29

  • go get command, 4, 1820

  • go get -u command, 2122

  • go help mod command, 2

  • go help test command, 477

  • go mod command, 2

  • go mod init command, 3

  • go test command, 202203

  • go tool cover command, 204205

  • go vet command, 71

  • golang.org/x/exp/constraints package, 326328

  • golang.org/x/sync/errgroup package, 434

  • go.mod file, 2

  • goroutines, 338. See also channels; contexts; synchronization

    • memory management, 339

    • messages and, 350351

    • runtime.GOMAXPROCS function, 341342

    • scheduling, 339342

    • usage example, 342

    • work sharing, 339340

    • work stealing, 341

  • go.sum file, 2021

  • Go-syntax of value, printing, 6970

  • graceful shutdown

    • implementing, 365367

    • listening for shutdown confirmation, 368370

    • listening for system signals, 367368

    • rules for, 363

    • timing out nonresponsive shutdown, 370371

  • growing slices, 9294, 9799

H

  • handling errors, 262263

  • heap, 183

  • helper functions

    • in context package, 378

    • test helpers, 222

    • .hidden directory, 467468

    • HTML coverage reports, 205206

    • http.HandlerFunc type, 181

I

  • identifiers. See constants; variables

  • if statements, 129130

  • _ignore directory, 467468

  • implementation-specific numeric types, 31

  • implicit implementation of interfaces, 234235

  • import aliases, 16

  • import keyword, 14

  • importing

    • modules, 1316, 22

    • packages, 1316

      • cyclical imports, 2425

      • multiple versions, 2324

      • for side effects, 164166

  • incrementing integers, 122

  • indexing

    • arrays, 8182

    • avoiding out-of-bounds errors, 287289

    • slices, 8182

  • inferred typing, 5051

  • inheritance, 181182

  • init functions, 162163

  • initializing

  • instantiating generics, 320321

  • int numeric type, 3132

  • int8 numeric type, 31

  • int16 numeric type, 31

  • int32 numeric type, 31

  • int64 numeric type, 31

  • integers

    • formatting with formatting verbs, 6366

    • incrementing/decrementing, 122

    • padding, 6466

  • interfaces

  • interpreted languages, 2829

  • interpreted string literals, 35

  • io.Reader interface, 503505

  • io.ReadWriteCloser interface, 249250

  • io.Writer interface, 237240, 503505

  • IsError interface, 305306

  • iterating over UTF-8 characters, 3840

  • iteration

J

  • JSON encoding with struct tags, 173176

K

L

  • len function, 91, 9697, 112

  • length

  • listener function, 353

  • listening

    • for cancellation events on contexts, 375376

    • to channels

      • with select statements, 348349

      • for shutdown confirmation, 368370

      • for system signals, 367368

      • for context cancellation confirmation, 400405

      • for error group cancellation, 439440

      • for system signals with contexts, 411413

    • listing map keys, 126127

    • listings

      • accessing struct fields, 172173

      • adding negative number of goroutines to waitgroups, 425426

      • adding positive number of goroutines to waitgroups, 423424

      • adding zero number of goroutines to waitgroups, 424425

      • a.go file, 8

      • All method, 208209

      • anonymous functions, 150

      • any keyword, 242243

      • appending to files, 501503

      • appending to slices, 8690

      • array of four strings, 78

      • AsError interface, 303304

      • asserting interface implementation, 242

      • assigning functions to variables, 283284

      • assigning multiple values, 4546

      • assigning variables, 4142

      • bad folder files listing, 8

      • bad identifier names, 52

      • bad package build, 9

      • bad.go file, 1011

      • bar/bar.go file, 15

      • bar.go file, 2425

      • b.go file, 8

      • blocking/unblocking channels, 345

      • boolean equality comparison, 129130

      • break keyword, 107108

      • buffered channel message delivery, 361362

      • buffered channel usage example, 359360

      • calling functions without interfaces, 235237

      • calling generics, 316317

      • calling methods, 177

      • cap function, 92, 112113

      • capturing and returning panic values, 269273

      • casting values, 45

      • channel usage example, 345347

      • checking for nil receivers, 193, 274275

      • checking type assertions, 285287

      • closing already-closed channels, 357

      • closing channels, 353354, 463466

      • closures, 149150

      • code coverage, 203

      • compilation errors, 18, 2930, 34

      • complex if/else if statements, 135136

      • complex values in maps, 123124

      • concrete types versus interfaces, 231233

      • constraints.Integer constraint, 327

      • constraints.Ordered constraint, 328

      • constraints.Signed constraint, 327

      • context key collisions, 389390

      • context key security, 394396

      • context package, 373, 378

      • context.Background function, 378380

      • context.Canceled error, 409410

      • context.CancelFunc function, 397398, 402403

      • context.Context interface, 374

      • context.Context.Deadline method, 374

      • context.Context.Done method, 375376, 398399

      • context.Context.Err method, 376, 408409

      • context.Context.Value method, 376378

      • context.DeadlineExceeded error, 410411

      • context.WithCancel function, 397

      • context.WithDeadline function, 405406

      • context.WithTimeout function, 407408

      • context.WithValue function, 385386

      • continue keyword, 107108

      • converting arrays to slices, 102104

      • converting strings to/from numbers, 7475

      • copying map values on insert, 124125

      • copying slices, 101102

      • coverage profiles, 203204

      • coverage profiles on per-function basis, 207

      • crafting failure messages, 201202

      • creating arrays versus slices, 79

      • creating directory structure, 486487

      • creating files/directories, 499500

      • custom data type usage, 168

      • custom string key types, 391393

      • cyclical imports, 2425

      • data types in arrays and slices, 83

      • debug.PrintStack function, 307

      • debug.Stack function, 307

      • declaring constants, 48

      • declaring methods, 176

      • declaring new data types, 167

      • declaring slices, 9596

      • declaring variables, 40

      • default blocks in switch statements, 138139

      • deferred function calls and exit/fatal message, 159

      • deferred function calls and panics, 158

      • deferring anonymous functions, 159160

      • deferring function calls, 156

      • deferring function calls with multiple returns, 156157

      • defining custom errors, 292293

      • defining interfaces, 243247

      • defining structs, 169

      • defining test helpers, 223226

      • defining type constraints, 321322

      • delete function, 118

      • deleting map keys, 118119

      • demo module files listing, 15

      • detecting race conditions in tests, 447449

      • disabling test caching, 221

      • discarding variables, 47

      • do while loops, 109

      • else if statements, 132

      • else statements, 130131

      • embed package, 517

      • embedding files as strings/byte slices, 522

      • embedding files in binaries, 520521

      • embed.FS type, 518519

      • empty interface, 242, 243

      • encoding with struct tags, 173175

      • errgroup.Group type, 436, 441442

      • errgroup.Group.Go method, 437

      • errgroup.Group.Wait method, 437439

      • errgroup.WithContext function, 439440

      • error interface, 261, 289

      • error types, 296

      • error usage example, 263264

      • errors as values, 260261

      • errors with file path helpers, 488489

      • errors with formatting verbs, 70

      • errors.As function, 302303

      • errors.Is function, 305, 489

      • errors.New function, 261

      • errors.Unwrap function, 297301

      • escape characters and , 62

      • escaping escape characters, 6263

      • exceptions, 259260

      • expanding slices with variadic operator, 154

      • explicit argument indexes, 7172

      • explicit implementation of interfaces, 233234

      • exporting context keys, 393394

      • exporting variables, 57

      • external dependencies file structure, 20

      • failing fast when testing, 221

      • fallthrough keyword, 139140

      • file tree example, 467468

      • filepath package, 484

      • filepath.Base function, 486

      • filepath.Dir function, 485

      • filepath.Ext function, 485

      • filepath.Join function, 505506

      • filepath.WalkDir function, 473474, 475477

      • fixing walk tests, 497500

      • flect.Dasherize function, 17

      • float formatting verbs, 66

      • fmt package, 58

      • fmt.Errorf method, 262

      • fmt.Fprint function, 59

      • fmt.Print function, 59

      • fmt.Println function, 60

      • fmt.Sprint function, 5859

      • fmt.Stringer interface, 241

      • foo type, 25

      • foo/bar/bar.go file, 15

      • foo.go file, 13

      • for loops, 105

      • fs package, 507

      • fs.DirEntry type, 469470

      • fs.File interface, 509

      • fs.FileInfo interface, 471472

      • fs.FS interface, 508

      • fs.SkipDir error, 478479

      • fstest.MapFile type, 509, 515

      • fstest.MapFS type, 514, 515517

      • fs.WalkDir function, 510512

      • fs.WalkDirFunc function, 474475

      • function arguments, 142143

      • function definitions, 141

      • function types, 180181

      • functions accepting other function arguments, 151

      • functions as arguments, 148149

      • functions as variables, 147148

      • generic types, 332334

      • generics, 316

      • getting pointers, 185186

      • //go:embed directive, 518, 521522

      • go get command, 4, 1819

      • go help test command, 477

      • go mod command, 2

      • go mod init command, 3

      • Go module files listing, 1

      • go tool cover command, 204205

      • go vet command, 71

      • golang.org/x/exp/constraints package, 326

      • golang.org/x/sync/errgroup package, 434

      • go.mod file, 2, 18

      • go.mod file updates, 20

      • good folder files listing, 78, 1112

      • goroutine usage example, 342

      • goroutines, 338

      • go.sum file, 21

      • growing slices, 9294

      • handling errors, 263

      • HTML coverage reports, 205206

      • http.HandlerFunc type, 181

      • if statements, 130

      • implementing graceful shutdown, 365367

      • implementing interfaces, 247249

      • implicit implementation of interfaces, 234235

      • importing modules with semantic versioning, 22

      • importing multiple packages, 14, 2324

      • improper mutex usage, 456459

      • indexing arrays and slices, 8182

      • inferred typing, 5051

      • inheritance, 182

      • init function, 162163

      • init functions for side effects, 165166

      • initializing arrays and slices, 80

      • initializing interfaces, 280283

      • initializing maps, 113, 276278

      • initializing pointers, 278280

      • initializing structs, 169172

      • initializing variables, 44

      • instantiating generics, 320321

      • integer formatting verbs, 64

      • interface usage example, 238239

      • interpreted string literals, 35

      • io.ReadWriteCloser interface, 249250

      • io.Writer interface, 237, 239240

      • IsError interface, 305306

      • iterating over maps, 116118

      • iterating over UTF-8 characters, 3940

      • key resolution in contexts, 386387

      • len function, 91, 112

      • listener function, 353

      • listening for shutdown confirmation, 368370

      • listening for system signals, 367368, 412413

      • listing map keys, 126127

      • log.Fatal function, 273

      • logging in tests, 215

      • main.go file, 14, 17

      • make function, 95, 113, 344

      • make function with append function, 97

      • make function with length and capacity, 9697

      • managing goroutines with sync.WaitGroup, 434436

      • map keys, 114115, 311315

      • messages and goroutines, 350351

      • method receivers, 188189

      • methods on third-party types, 178179

      • mixing type and method constraints, 331

      • modifying constants, 48

      • modifying variables, 4849

      • multiple generic types, 317319

      • multiple goroutines for one task, 419420

      • multiple init functions, 163164

      • multiple interfaces, 241

      • multiple Println arguments, 61

      • multiple return arguments, 144145

      • multiple type constraints, 322323

      • mutating subsets of slices, 100

      • name conflicts between variables and packages, 5455

      • name conflicts when importing packages, 1516

      • named returns, 146147

      • names folder files listing, 13

      • naming files, 9

      • naming packages, 56

      • naming variables, 53

      • new function, 189190

      • new lines, 60

      • nil receivers, 191192

      • nodal hierarchy for contexts, 381384

      • nonexistent map keys, 120121

      • order of execution for deferred function calls, 157

      • order of execution for init functions, 164

      • os.Create function, 492494, 495496

      • os.DirEntry type, 468

      • os.DirFS function, 512

      • os.FileMode type, 482

      • os.Interrupt type, 365

      • os.Mkdir function, 481483

      • os.MkdirAll function, 490492

      • os.OpenFile function, 501

      • os.ReadDir function, 468, 470

      • os.ReadFile function, 494495

      • os.Signal type, 364

      • os.Stat function, 472473

      • overflow example, 3334

      • package folder structure, 4, 67

      • padding integers, 6466

      • panic from out-of-bounds error, 264265, 287289

      • panic function, 265

      • passing by value, 184

      • path package, 54

      • pointer usage, 187

      • printing stack traces, 308

      • printing value Go-syntax, 6970

      • printing value type, 67

      • printing values, 6869

      • problem solved by single task execution, 460462

      • -race flag, 445446

      • range keyword, 106

      • range loops, 348

      • raw string literals, 3637

      • reading closed buffered channels, 362363

      • reading closed channels, 354355

      • reading files, 504505

      • receiving pointers, 184

      • recover function, 266

      • recovering from panics, 266269

      • resolving name conflicts, 5557

      • return arguments, 143

      • returning errors, 144

      • runes, 38

      • running package tests, 213

      • running specific tests, 218219

      • running tests with subpackages, 213214

      • runtime.GOMAXPROCS function, 341342

      • saturation example, 3435

      • scope of deferred functions, 160162

      • select statements, 349

      • sending TEST_SIGNAL signal, 416

      • sending/receiving values via channels, 345

      • setting and retrieving map values, 111112

      • shared access example, 443445, 449

      • signal.Notify function, 364

      • signal.NotifyContext function, 412

      • slice of four strings, 7879

      • smtp package, 7

      • sort package, 127128

      • sorting map keys, 128129

      • sound package, 7

      • standard error examples, 290291

      • starting concurrent monitors, 401402

      • strconv.ParseInt function, 73

      • string formatting verbs, 63

      • string keys in contexts, 388

      • struct tags, 173

      • structs as map keys, 115116

      • structure of subtests, 211212

      • structure of table driven tests, 208

      • subsets of slices, 99

      • subtests, 212213

      • switch statements, 136138

      • sync package, 447

      • sync.Locker interface, 450

      • sync.Locker.Lock method, 450

      • sync.Locker.Unlock method, 451

      • sync.Mutex type, 451453

      • sync.Once type, 459460, 462463, 465

      • sync.RWMutex type, 453455

      • sync.Waitgroup type, 421, 432433

      • sync.WaitGroup.Add method, 422

      • sync.WaitGroup.Done method, 427431

      • sync.WaitGroup.Wait method, 422

      • syntactic sugar for methods, 177178

      • table driven tests, 209210

      • test file naming conventions, 196

      • test function signatures, 196

      • testing map key presence, 122123

      • testing system signals, 414416

      • testing.Short function, 216217

      • testing.T type, 197

      • testing.TB interface, 222223

      • testing.TB.Cleanup method, 227229

      • testing.TB.Helper method, 226

      • testing.T.Error method, 198200

      • testing.T.Fatal method, 198, 200201

      • testing.T.Parallel method, 217218

      • testing.T.Run method, 211

      • theoretical representation of slices, 91

      • time.Ticker function, 351

      • timing out nonresponsive shutdown, 370371

      • timing out tests, 219220

      • type assertion usage example, 255257

      • type assertions, 251, 329330

      • type assertions with switch statements, 253254

      • type definitions for arrays and slices, 8384

      • typed constants, 49

      • underlying type constraints, 324326

      • unidirectional channels, 352

      • uninitialized maps, 114

      • unused variables, 47

      • Unwrapper interface, 299

      • unwrapping errors, 297301

      • updating complex values in maps, 125126

      • updating dependencies, 2122

      • UTF-8 character example, 3738

      • validating type assertions, 252

      • variable declarations, 2728

      • variable scope in if statements, 132134

      • variadic arguments, 151153

      • verbose test output, 214

      • verifying concrete types with type assertions, 252

      • when to use variadic arguments, 154156

      • work sharing, 339340

      • work stealing, 341

      • wraparound example, 33

      • wrapping errors, 294297

      • writing to closed channels, 358

      • zero values for arrays and slices, 81

      • zero values for closed channels, 355357

      • zero values for maps, 121122

      • zero values for variables, 4244

    • lists

      • arrays

        • converting to slices, 102104

        • creating, 79

        • data types in, 8283

        • indexing, 8182

        • initializing, 7980

        • iteration over, 105

        • purpose of, 77

        • setting values of, 85

        • slices versus, 7779

        • subsets of, 103104

        • type definitions, 8384

        • zero values, 81

      • slices

        • appending to, 8590, 97

        • arrays versus, 7779

        • converting arrays to, 102104

        • copying, 101102

        • creating, 79, 9597

        • data types in, 8283

        • declaring, 9596

        • growing, 9294, 9799

        • indexing, 8182

        • initializing, 7980

        • iteration over, 105

        • length and capacity, 9192, 9697

        • purpose of, 77

        • setting values of, 85

        • subsets of, 99100

        • theoretical representation of, 91

        • type definitions, 8384

        • zero values, 81

    • locking resources

      • improper mutex usage, 455459

      • sync.Locker interface, 449451

      • sync.Mutex type, 451453

      • sync.RWMutex type, 453455

    • log.Fatal function, 273

    • logging in tests, 215

    • logic. See boolean logic; control structures

    • loops

      • do while loops, 108109

      • for loops

        • appending to slices, 8889

        • break keyword, 107108

        • continue keyword, 107108

        • explained, 104105

        • iteration over arrays and slices, 105

        • range keyword, 105106

      • over channels, 348

M

  • make function, 9597, 113, 278, 344

  • maps

  • mathematical operators, 134

  • memory management, 28

    • array and slice memory spaces, 85

    • cleaning up test helpers, 227229

    • goroutines, 339

    • heap, 183

    • pointers and, 190191

    • stack, 183, 186

  • message delivery, buffered channels and, 360362

  • message queues, channels versus, 349351

  • metadata on files, 471473

  • methods

    • calling, 177

    • declaring, 176

    • on functions, 181

    • functions versus, 177

    • inheritance, 181182

    • mixing type and method constraints, 331

    • syntactic sugar, 177178

    • for test failures, 198202

    • on third-party types, 178179

    • value versus pointer receivers, 188189

  • mocking file systems, 513517

  • modifying

    • constants, 48

    • embedded files, 521522

    • variables, 4849

    • modules, 14

      • commands for, 2

      • definition of, 1

      • dependencies, 1622

      • go.sum file, 2021

      • requiring, 1820

      • updating, 2122

      • usage, 1718

    • importing, 1316, 22

    • initializing, 3

    • VCS and, 34

  • monitors, starting concurrent, 401402

  • multiline strings in raw string literals, 3637

  • multiple arguments with Println function, 61

  • multiple directories, creating, 489492

  • multiple generic types, 317320

  • multiple init functions, 163164

  • multiple interfaces, 241

  • multiple packages

    • in folder structure, 198

    • importing, 14, 2324

  • multiple return arguments, 144145

  • multiple type constraints, 322323

  • multiple values, assigning to variables, 4546

  • mutating subsets of slices, 100

  • mutexes, synchronization with

    • detecting race conditions in tests, 447449

    • improper usage, 455459

    • locking resources, 449451

    • sync.RWMutex type, 453455

    • usage example, 451453

N

  • name conflicts

    • between variables and packages, 5357

    • when importing packages, 1416

  • named returns, 145147

  • names of files/directories, retrieving, 486

  • naming

    • files, 9

    • packages, 56

    • test files, 195196

    • variables, 5157

      • bad examples, 52

      • capitalization, 57

      • package name conflicts, 5357

      • rules for, 5152

      • styles for, 53

  • negative number of goroutines, adding to waitgroups, 425426

  • new function, 189190

  • new lines

    • escape characters for, 6162

    • printing, 60

  • nil receivers, 191193, 274275

  • nil value for variables, 42

  • nodal hierarchy for contexts, 381

  • nonexistent map keys, 120121

  • nonresponsive shutdown, timing out, 370371

  • numbers, converting strings to/from, 7275

  • numeric types, 3135

    • architecture-independent, 31

    • choosing, 32

    • implementation-specific, 31

    • overflow versus wraparound, 3234

    • saturation, 3435

O

  • operators

  • order of execution

    • for deferred function calls, 157

    • for init functions, 164

  • os.Create function, 492494, 495496, 499500

  • os.DirEntry type, 468

  • os.DirFS function, 512

  • os.FileMode type, 482

  • os.Interrupt type, 365

  • os.Mkdir function, 481483, 488489

  • os.MkdirAll function, 489492, 499500

  • os.OpenFile function, 501

  • os.ReadDir function, 468, 470

  • os.ReadFile function, 494495

  • os.Signal type, 364

  • os/signals package, 363365

  • os.Stat function, 472473

  • out-of-bounds errors, 8182, 264265, 287289

  • overflow in numeric types, 3234

P

  • package aliases, resolving name conflicts, 16, 5657

  • package keyword, 5

  • packages, 46

    • definition of, 4

    • dependencies, 1622

    • folder structure, 4, 613

      • multiple packages, 79

      • naming files, 9

      • organization, 913

    • importing, 1316

      • cyclical imports, 2425

      • multiple versions, 2324

      • for side effects, 164166

    • initializing. See init functions

    • name conflicts

      • with variables, 5357

      • when importing, 1416

    • naming, 56

    • running tests, 213, 217

  • padding integers, 6466

  • panic function, 265

  • panics

    • avoiding, 273

      • assigning functions to variables, 283284

      • checking for nil receivers, 274275

      • checking type assertions, 284287

      • initializing interfaces, 280283

      • initializing maps, 275278

      • initializing pointers, 278280

      • from out-of-bounds errors, 287289

    • capturing and returning values, 269273

    • closing channels, 464465

    • decrementing sync.WaitGroup, 431

    • deferred function calls and, 158

    • log.Fatal function, 273

    • from out-of-bounds errors, 264265, 287289

    • raising, 265

    • recovering from, 265269

  • parallelism

    • concurrency versus, 335336

    • running tests in parallel, 217218

  • ParseInt function, 73

  • passing

  • path for importing packages, 1314

  • performance, pointers and, 190191

  • pointer receivers, value receivers versus, 188189

  • pointers, 182183

  • positive number of goroutines, adding

  • to waitgroups, 423424

  • Print functions, 59

  • printing

    • pointers, 185

    • runes, 38

    • stack traces, 308

    • strings

      • Fprint functions, 59

      • multiple Println arguments, 61

      • new lines, 60

      • Print functions, 59

    • structs, 169

    • values

      • with formatting verbs, 6769

      • Go-syntax of value, 6970

      • type of value, 67

  • Println function

    • adding new lines, 60

    • multiple arguments, 61

Q

  • quotation marks for string literals, 35

R

S

  • saturation in numeric types, 3435

  • scheduling goroutines, 339342

  • scope

    • of APIs, 10

    • of deferred functions, 160162

    • of variables in if statements, 132134

  • security of context keys, 394396

  • select statements, 348349

  • selecting numeric types, 32

  • semantic versioning, 22

  • sending

    • TEST_SIGNAL signal, 416

    • values via channels, 344345

  • shared access

  • short tests, 216217

  • shutdown, graceful

    • implementing, 365367

    • listening for shutdown confirmation, 368370

    • listening for system signals, 367368

    • rules for, 363

    • timing out nonresponsive shutdown, 370371

  • shutdown confirmation, listening for, 368370

  • side effects, init functions for, 164166

  • signal.Notify function, 364

  • signal.NotifyContext function, 412

  • single execution of task

  • skipping

  • slices

    • appending to, 8590, 97

    • arrays versus, 7779

    • avoiding out-of-bounds errors, 287289

    • converting arrays to, 102104

    • copying, 101102

    • creating, 79, 9597

    • data types in, 8283

    • declaring, 9596

    • embedding files as, 522

    • expanding with variadic operator, 153154

    • growing, 9294, 9799

    • indexing, 8182

    • initializing, 7980

    • iteration over, 105

    • length and capacity, 9192, 9697

    • purpose of, 77

    • setting values of, 85

    • subsets of, 99100

    • theoretical representation of, 91

    • type definitions, 8384

    • zero values, 81

  • sort package, 127129

  • sorting map keys, 127129

  • specific tests, running, 218219

  • Sprint functions, 5859

  • stack, 183, 186

  • stack traces, 307308

  • standard errors, custom errors versus, 289291

  • starting concurrent monitors, 401402

  • static typing, 2728

  • strconv package, 7275

  • string keys in contexts, 388

  • string literals, 3537

    • converting to/from numbers, 7275

    • embedding files as, 522

    • formatting

      • errors with formatting verbs, 7071

      • escape characters, 6163

      • explicit argument indexes, 7172

      • with formatting verbs, 61, 63

      • list of functions, 5758

      • Sprint functions, 5859

    • interpreted, 35

    • printing

      • Fprint functions, 59

      • multiple Println arguments, 61

      • new lines, 60

      • Print functions, 59

    • quotation marks for, 35

    • raw, 3637

  • struct tags, encoding with, 173176

  • structs, 167

  • structural typing, 235

  • subdirectories, creating, 481483

  • subpackages, running tests with, 213214

  • subsets

  • subtests, 211213

  • switch statements, 135138

    • default blocks, 138139

    • fallthrough keyword, 139140

    • type assertions with, 252254

  • sync package, 447

  • synchronization

    • error groups

      • errgroup.Group type, 436, 440442

      • errgroup.Group.Go method, 437

      • errgroup.Group.Wait method, 437439

      • errgroup.WithContext function, 439440

      • golang.org/x/sync/errgroup package, 434

      • problem solved by, 434436

    • with mutexes

      • detecting race conditions in tests, 447449

      • improper usage, 455459

      • locking resources, 449451

      • sync.RWMutex type, 453455

      • usage example, 451453

    • race conditions

    • single execution of task closing channels, 463466

    • problem solved by, 460462

    • sync.Once type, 459460, 462463

  • sync package, 447

  • waitgroups

    • problem solved by, 419421

    • sync.Waitgroup type, 421, 432433

    • sync.WaitGroup.Add method, 422426

    • sync.WaitGroup.Done method, 426431

    • sync.WaitGroup.Wait method, 421422

  • sync.Locker interface, 449451

  • sync.Locker.Lock method, 450

  • sync.Locker.Unlock method, 451

  • sync.Mutex type, 451453

  • sync.Once type, 459460, 462463, 465

  • sync.RWMutex type, 453455

  • sync.Waitgroup type, 421, 432433

  • sync.WaitGroup.Add method, 422426

  • sync.WaitGroup.Done method, 426431

  • sync.WaitGroup.Wait method, 421422

  • syntactic sugar for methods, 177178

  • system signals

  • capturing, 363

    • implementing graceful shutdown, 365367

    • listening for shutdown confirmation, 368370

    • listening for system signals, 367368

    • os/signals package, 363365

    • timing out nonresponsive shutdown, 370371

  • listening with contexts, 411413

  • testing, 413416

T

  • tab character, escape characters for, 6162

  • table driven testing, 206207

  • test caching, disabling, 221

  • test failures

    • crafting failure messages, 201202

    • marking, 198

    • testing.T.Error method, 198200

    • testing.T.Fatal method, 200201

  • test files, naming, 195196

  • test function signatures, 196

  • test helpers, 222

  • TEST_SIGNAL signal, sending, 416

  • testdata directory, 467468, 480481

  • testing

  • testing framework, 195

    • code coverage

      • coverage profiles, 203204

      • editor support for, 206

      • go test command, 202203

      • go tool cover command, 204205

      • HTML coverage reports, 205206

    • running tests, 213

    • table driven testing, 206207

    • test failures

      • crafting failure messages, 201202

      • marking, 198

      • testing.T.Error method, 198200

      • testing.T.Fatal method, 200201

    • test file naming conventions, 195196

    • test helpers, 222

    • testing.T type, 196197

  • testing.Short function, 216217

  • testing.T type, 196197

  • testing.TB interface, 222223

  • testing.TB.Cleanup method, 227229

  • testing.TB.Helper method, 226

  • testing.T.Error method, 198200

  • testing.T.Fatal method, 198, 200201

  • testing.T.Log method, 215

  • testing.T.Parallel method, 217218

  • testing.T.Run method, 211

  • third-party types, methods on, 178179

  • threads. See goroutines

  • time.Ticker function, 351

  • timing out

  • truncating files, 495497

  • type assertions, 250251

  • type constraints, 315317

  • type definitions for arrays and slices, 8384

  • type inference, 5051

  • type keyword, 167168

  • type of value, printing, 67

  • typed constants, 49

  • types. See data types; interfaces

U

  • uint numeric type, 3132

  • uint8 numeric type, 31

  • uint16 numeric type, 31

  • uint32 numeric type, 31

  • uint64 numeric type, 31

  • uintptr numeric type, 31

  • unblocking channels, 343344

  • underlying type constraints, 324326

  • unidirectional channels, 351352

  • uninitialized maps, 114

  • untyped constants, 5051

  • unused variables, 4647

  • Unwrapper interface, 299

  • unwrapping errors, 297301

  • updating

    • complex values in maps, 125126

    • dependencies, 2122

  • UTF-8 characters, 3740

    • iterating over, 3840

    • runes, 38

V

  • validatable interfaces, 250

  • validating type assertions, 251252

  • value receivers, pointer receivers versus, 188189

  • values

    • of arrays and slices, setting, 85

    • converting strings to/from, 7275

    • errors as, 259261

      • error interface, 261

      • errors.New function, 261

      • fmt.Errorf method, 262

      • handling, 262263

      • usage example, 263264

    • lists of. See lists

    • in maps

      • complex values, 123124

      • copying on insert, 124125

      • updating complex values, 125126

    • panic values, capturing and returning, 269273

    • printing

      • with formatting verbs, 6769

      • Go-syntax of value, 6970

      • type of value, 67

    • receiving pointers, 184

    • retrieving from contexts, 376378

    • sending/receiving via channels, 344345

    • wrapping contexts with, 382, 384386

  • var keyword, 40

  • variables, 4047

    • assigning, 4142

      • functions to, 283284

      • multiple values, 4546

    • closures, 149150

    • declaring, 40, 4445

    • discarding, 47

    • exporting, 57

    • functions as, 147148

    • getting pointers, 185186

    • initializing, 4445

    • modifying, 4849

    • naming, 5157

      • bad examples, 52

      • capitalization, 57

      • package name conflicts, 5357

      • rules for, 5152

      • styles for, 53

    • nil value, 42

    • scope in if statements, 132134

    • unused, 4647

    • values

      • converting strings to/from, 7275

      • lists of. See lists

      • printing, 6769

      • printing Go-syntax of, 6970

      • printing type of, 67

    • zero values, 4244

  • variadic arguments, 151152

  • VCS (version control systems), modules and, 34

  • verbose test output, 214

  • verbs, formatting, 61

    • errors with, 7071

    • escape characters, 6163

    • explicit argument indexes, 7172

    • floats, 6667

    • integers, 6366

    • strings, 63

    • value type printing, 67

W

  • waitgroups

    • problem solved by, 419421

    • sync.Waitgroup type, 421, 432433

    • sync.WaitGroup.Add method, 422426

    • sync.WaitGroup.Done method, 426431

    • sync.WaitGroup.Wait method, 421422

  • walk test errors, fixing, 497500

  • walking directories, 473477, 510513

  • Windows filepaths, 505506

  • work sharing, 339340

  • work stealing, 341

  • wraparound in numeric types, 3234

  • wrapping

  • writing

    • to closed channels, 358

    • subtests, 212213

    • table driven testing, 208210

Z

  • zero number of goroutines, adding to waitgroups, 424425

  • zero values

    • for arrays and slices, 81

    • for closed channels, 355357

    • for maps, 121122

    • for variables, 4244

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset