Skip to content

golangatspeed/GoFasterExamples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Faster Example Code

The Go Faster book has many code examples to accompany the text. The book is completed, at 200+ pages, 48,000+ words and includes over 145 Go code examples.

I'm including all the examples here. There's no explainer text to accompany the examples, that's in the book, but the examples are designed to strip away the noise to highlight the principles being demonstrated.

Most examples in the book are executable on the Go Playground. Links to the Go Playground examples are included so readers can quickly inspect, alter and execute the code for themselves, since that's where the true learning is. Those Go Playground links are also included in this repository.

This resource may well be enough "as-is", but if not, please consider purchasing my book. It's inexpensive and thorough, and I'm confident it will help you learn Go, faster.

One caveat. Not all examples are demonstrations of best practice and some are specifically set up to generate compile time and runtime errors :)

Contents

Chapter 1 - Introduction to Go

  1. Visibility modifiers
  2. Comment styles
  3. Making the most of comments in documentation
  4. An 'example' function

Chapter 2 - The Go command line interface (CLI)

No Go code examples used

Chapter 3 - Structure of a Go program

  1. Hello World in Go (Go Playground: https://go.dev/play/p/HbshOM2vDe)
  2. How not to use aliasing (Go Playground: https://go.dev/play/p/zuXJ0If5OFn)
  3. The dot import prefix (Go Playground: https://go.dev/play/p/SEUbOnOb6wH)
  4. Simple package

Chapter 4 - Project organisation

No Go code examples used

Chapter 5 - Dependency management

No Go code examples used

Chapter 6 - Variables and constants

  1. Recommended variable declaration styles
  2. All variable declaration styles (Go Playground: https://go.dev/play/p/c8eFFqUfVTz)
  3. Discarding with the blank identifier (Go Playground: https://go.dev/play/p/NYjfpotoOtx)
  4. Loose typing of number constants (Go Playground: https://go.dev/play/p/MC8HsTfnVz9)
  5. Managing number constants manually (Go Playground: https://go.dev/play/p/EVMSF3BCCn8)
  6. Managing number constants with iota (Go Playground: https://go.dev/play/p/h4fNoptnCJZ)
  7. Rebasing the numbering from 1
  8. Non-linear constant sequences (Go Playground: https://go.dev/play/p/8QypEyOoxT2)
  9. Values used in place of named constants (Go Playground: https://go.dev/play/p/n-POyR0-k7a)
  10. Global and function scope (Go Playground: https://go.dev/play/p/V7OT8HX_XMP)
  11. Local scope in control structure (Go Playground: https://go.dev/play/p/edNkZtJuCPk)
  12. Pass by value (Go Playground: https://go.dev/play/p/uESKRz6r-tS)
  13. Pass by reference (Go Playground: https://go.dev/play/p/7Ne6OPnCslN)
  14. Obtaining a pointer directly (Go Playground: https://go.dev/play/p/t3iJJ47laIt)

Chapter 7 - Data types

  1. Floating point number addition problem (Go Playground: https://go.dev/play/p/_uoAHQeoh9h)
  2. String cut using byte slice (Go Playground: https://go.dev/play/p/egXJA0gm97x)
  3. Identical strings with different lengths (Go Playground: https://go.dev/play/p/ujUnmx-LsWu)
  4. Unicode codepoint with UTF-8 code (Go Playground: https://go.dev/play/p/luDIj6DwPAG)
  5. Safely obtaining length of a string (Go Playground: https://go.dev/play/p/wTnoddQnjvJ)
  6. Array length, capacity and element initialisation (Go Playground: https://go.dev/play/p/D_hs2NHHoAs)
  7. Array length is part of its type definition (Go Playground: https://go.dev/play/p/xgNuJjZQHZM)
  8. Set array length with the spread operator (Go Playground: https://go.dev/play/p/bxX2YplIPa8)
  9. Named custom struct type and anonymous struct (Go Playground: https://go.dev/play/p/gWnMhWF_OVK)
  10. Unnamed field properties (Go Playground: https://go.dev/play/p/hun_q6dx-VC)
  11. Get and set struct fields (Go Playground: https://go.dev/play/p/gM5fADxC9gB)
  12. Composition and struct embedding (Go Playground: https://go.dev/play/p/6DSmzLgW3Xo)
  13. Alignment and impact on memory (Go Playground: https://go.dev/play/p/q7GJI1xfEqb)
  14. Trying to use a nil map (Go Playground: https://go.dev/play/p/dOBBmtyVYuV)
  15. Creating an empty map (Go Playground: https://go.dev/play/p/kDhy20FhD6B)
  16. Map passed by value (Go Playground: https://go.dev/play/p/i20LKvkaC0J)
  17. Map passed by reference (Go Playground: https://go.dev/play/p/lgiCDOYzSrj)
  18. Working with maps (Go Playground: https://go.dev/play/p/6-ht0hCoQFj)
  19. Unsafe map access (Go Playground: https://go.dev/play/p/9jX2B7x1eha)
  20. Safe map access (Go Playground: https://go.dev/play/p/a4zZyEM8viF)
  21. Creating a slice (Go Playground: https://go.dev/play/p/922R9tr-3Aq)
  22. Slice not behaving as a reference type (Go Playground: https://go.dev/play/p/0cOx0y5Xc_L)
  23. Safely return the new slice to caller (Go Playground: https://go.dev/play/p/A-Lg98S0GRJ)
  24. Slice behaving as a reference type (Go Playground: https://go.dev/play/p/CvKjbcRyOAr)
  25. Reslicing & working with slices (Go Playground: https://go.dev/play/p/Aiw7WhYGePH)
  26. Slices of slices (Go Playground: https://go.dev/play/p/NAWtoteoj5x)
  27. Using append (Go Playground: https://go.dev/play/p/WUPIBwbVCEL)
  28. Reslicing to remove a specified element from a slice (Go Playground: https://go.dev/play/p/e_IrhpQHBXE)
  29. Using copy to create a slice with new backing array (Go Playground: https://go.dev/play/p/YHVH-gE9lYW)
  30. New Slice backing array with additional capacity (Go Playground: https://go.dev/play/p/JMbFUxzyNzK)
  31. Send and receive on unbuffered channel (Go Playground: https://go.dev/play/p/f8M2OFSLnJA)
  32. The Stringer interface (Go Playground: https://go.dev/play/p/xl1phIcBej1)
  33. Implementing Stringer on a custom struct type (Go Playground: https://go.dev/play/p/Oa4sBfwsx78)
  34. Simple transposition error (Go Playground: https://go.dev/play/p/XhTIz_A-dxd)
  35. Type safety using custom types (Go Playground: https://go.dev/play/p/6UQcR54_wYX)
  36. Implementing the Stringer interface on a custom type (Go Playground: https://go.dev/play/p/RSGm_FPhf6L)
  37. Type conversion examples (Go Playground: https://go.dev/play/p/FOkyY6XJp1q)
  38. This won't print what you expect (Go Playground: https://go.dev/play/p/GHVwfyD-mlH)
  39. fmt.Sprintf to the rescue (Go Playground: https://go.dev/play/p/b6APk-xVK9X)
  40. Package strconv examples (Go Playground: https://go.dev/play/p/5kW2FyxBGgU)
  41. A custom BoolToI helper function (Go Playground: https://go.dev/play/p/gseJ2vzyDSc)

Chapter 8 - Managing program flow

  1. Using goto to restart function execution (Go Playground: https://go.dev/play/p/5dsPU41vPQ7)
  2. Simple function with conditional if/else logic (Go Playground: https://go.dev/play/p/hUPNR_4gRy_6)
  3. Eliminating the else statements (Go Playground: https://go.dev/play/p/lJm4fJ3REPa)
  4. Short-form if statement (Go Playground: https://go.dev/play/p/qmoU-gAtzCw)
  5. Simple switch with default (Go Playground: https://go.dev/play/p/3FiTf9lGEDd)
  6. Expressionless switch statement (Go Playground: https://go.dev/play/p/cx8jV4QXHRI)
  7. Short-form switch with multiple match tests (Go Playground: https://go.dev/play/p/DjPtZbdigfm)
  8. Fallthrough to execute the next case (Go Playground: https://go.dev/play/p/hw1WLkjYPUb)
  9. Infinite loop with for (Go Playground: https://go.dev/play/p/ZBcHX0L11Qq)
  10. Three component loop with for (Go Playground: https://go.dev/play/p/IFE1-HGEGkQ)
  11. While equivalent using for (Go Playground: https://go.dev/play/p/oC25p-YNNBx)
  12. Do while equivalent with for (Go Playground: https://go.dev/play/p/SZUMlRfW3Fn)
  13. For each performed using idiomatic for range (Go Playground: https://go.dev/play/p/CVjj4pUtc2c)
  14. Using break to exit a loop (Go Playground: https://go.dev/play/p/o2vXkdAYNk3)
  15. Using continue to advance to next loop iteration (Go Playground: https://go.dev/play/p/qbVGN5S3hLY)
  16. Using errors.Is to handle different error values (Go Playground: https://go.dev/play/p/gb2eikJ9Hp6)
  17. A custom error type (Go Playground: https://go.dev/play/p/x7pZDTqda5O)
  18. Error wrapping and unwrapping (Go Playground: https://go.dev/play/p/UzPyJIGc1DH)
  19. Basic panic and recover (Go Playground: https://go.dev/play/p/9vmgVqKTbU5)
  20. Embedding log.Logger to augment its features (Go Playground: https://go.dev/play/p/ca17kF91BtL)

Chapter 9 - Digging deeper

  1. Passing values as variadic arguments (Go Playground: https://go.dev/play/p/NPutwf2aF4l)
  2. Anonymous and named return parameters (Go Playground: https://go.dev/play/p/RdVlD9CwJS4)
  3. Bug risk or not? (Go Playground: https://go.dev/play/p/gHxfLdGgTaX)
  4. Callback style functions (Go Playground: https://go.dev/play/p/KXSxlwcWJB4)
  5. Closure functions (Go Playground: https://go.dev/play/p/x8XnvAM6O9C)
  6. Pointer return for database connection (Go Playground: https://go.dev/play/p/0afFBh0GaFG)
  7. Escape analysis with benchmark (Go Playground: https://go.dev/play/p/rqTcK20eVqV)
  8. Escape analysis on function returns (Go Playground: https://go.dev/play/p/rC5x05MpKP5)
  9. Value and pointer receivers (Go Playground: https://go.dev/play/p/qTBFdsIDdCL)
  10. Simple file store
  11. Using the store
  12. Interface design
  13. Implementing store.NewStore
  14. Implementing store.ReadWriteDeleter
  15. Refactoring to use the interface
  16. SendNotification function
  17. A basic unit test
  18. Notifier service interface
  19. Modifying the notification code
  20. Modify main
  21. Creating a mock notifications service
  22. A better unit test
  23. Injecting makeCallToAPI
  24. An alternative mock and unit test
  25. Type assertion provides the concrete type (Go Playground: https://go.dev/play/p/n9njIiD7Kk1)
  26. Invalid type assertion, compiles but will panic (Go Playground: https://go.dev/play/p/j3egltwQKPo)
  27. Safely performing a type assertion (Go Playground: https://go.dev/play/p/3jfW7_hkRjM)
  28. Type switch style assertion with default case (Go Playground: https://go.dev/play/p/0ovjw0QFJtL)
  29. Using reflection to discover a variable's type (Go Playground: https://go.dev/play/p/HFJswkAEnB5)
  30. Using reflection to determine if variable is a value or pointer (Go Playground: https://go.dev/play/p/Y7p3BSuVGjN)
  31. Using reflection to work with a struct (Go Playground: https://go.dev/play/p/D1GKlMeHs0V)
  32. Performance overhead of reflection
  33. Adding two int64 integers (Go Playground: https://go.dev/play/p/XDU49bgxRs_j)
  34. Adding other integer types (Go Playground: https://go.dev/play/p/Q3SzagyF-CS)
  35. A single "sumIntAny" implementation (Go Playground: https://go.dev/play/p/GZ0JOBSwu9_n)
  36. A generic implementation of SumAny (Go 1.18+) (Go Playground: https://go.dev/play/p/jbzggpV7uM8)
  37. Using a type constraint list (Go 1.18+) (Go Playground: https://go.dev/play/p/yZemwfvY6ST)
  38. Creating a custom constraint (Go 1.18+) (Go Playground: https://go.dev/play/p/IxpNJBWPkZo)
  39. Using composition with constraints (Go 1.18+) (Go Playground: https://go.dev/play/p/2yARr7xNBG8)
  40. Allow any type with underlying type int (Go 1.18+) (Go Playground: https://go.dev/play/p/b8jNMrEtUcL)
  41. Multiple type parameters (Go 1.18+) (Go Playground: https://go.dev/play/p/DFeMUaFqBzG)

Chapter 10 - Concurrency

  1. Setting GOMAXPROCS during runtime (Go Playground:https://go.dev/play/p/ptFRl7NAoT_6)
  2. Where is the output? (Go Playground:https://go.dev/play/p/nd5nwv5b3ok)
  3. Using context.WithCancel() (Go Playground:https://go.dev/play/p/aAbmie_rtVV)
  4. Using context.WithTimeout() (Go Playground:https://go.dev/play/p/6y1hYI5pSdM)
  5. Using context.WithDeadline() (Go Playground:https://go.dev/play/p/MyjT3EVGkus)
  6. Sharing data via context (Go Playground:https://go.dev/play/p/7p504omEWsq)
  7. Waitgroup updating shared state (Go Playground:https://go.dev/play/p/Z3Rh0ufgiVr)
  8. WaitGroup updating shared state unsafely (Go Playground:https://go.dev/play/p/dW1kdCrqmCu)
  9. Mutex to guarantee exclusivity of access (Go Playground:https://go.dev/play/p/ZObOF_af_W9)
  10. Signalling channel for mutual exclusion (Go Playground:https://go.dev/play/p/JHiaz5Rjvii)
  11. Sizing the buffer to store results (Go Playground:https://go.dev/play/p/YVTWPzUYht9)
  12. Unidirectional channel types (Go Playground:https://go.dev/play/p/ztggMJsU0cj)
  13. Unbuffered channel for signalling (Go Playground:https://go.dev/play/p/ro8OHTv4G4x)
  14. Signalling by closing the channel (Go Playground:https://go.dev/play/p/ciX1rKK-XAV)
  15. Signalling multiple goroutines by closing the channel (Go Playground:https://go.dev/play/p/vHFbaV5Tyg3)
  16. Reading from a closed channel (Go Playground:https://go.dev/play/p/iedP9__fCzR)
  17. Reading from a channel with range (Go Playground:https://go.dev/play/p/Qdo3mWvKmmg)
  18. Reading from a closed channel with range (Go Playground:https://go.dev/play/p/X5kcrsmOpQW)
  19. Monitoring multiple channels with select (Go Playground:https://go.dev/play/p/L8d4dFaVta3)

Chapter 11 - Quality Assurance

  1. Example test with subtests (Go Playground:https://go.dev/play/p/2NNWfknTbXC)
  2. Examples with ordered and unordered output (Go Playground:https://go.dev/play/p/RRE5gXK3A87)
  3. Modify code to obtain a CPU profile
  4. Realtime profiling during program execution

Releases

No releases published

Packages

No packages published

Languages