repository
0.0.0-20240424125238-ed678f0c0998
Repository: https://github.com/ardanlabs/practical-go.git
Documentation: pkg.go.dev
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# README
Practical Go for Developers
This repo contains the material for the "Practical Go Foundations" class. The code & links are synced with the [online class](for Developers).
This is an assorted collection of exercises for teaching, not a real Go project.
Setting Up
We highly recommend that you set up a local working environment and follow along with the videos. To setup a local environment install the following:
- The Go SDK either from your package manager (
brew
,apt
,choco
...) or from here git
- And IDE such as VSCode with the Go extension or GoLand
However, if you want to jump right in, you can use GitHub codespaces with the course repository. To start a codespaces follow these steps:
- Click on the green "<> Code" button
- Select the "Codespaces" tab
- Click on the green "Create codespace on main" button
After a while you should have a new tab with Visual Studio Code already set up. You can read more about codespaces here.
Day 1
Agenda
- Strings & formatted output
- What is a string?
- Unicode basics
- Using fmt package for formatted output
- Calling REST APIs
- Making HTTP calls with net/http
- Defining structs
- Serializing JSON
- Working with files
- Handling errors
- Using defer to manage resources
- Working with io.Reader & io.Writer interfaces
Code
- hw.go - Hello World
GOOS=drawin go build
(alsoGOARCH
)
- banner.go - Strings & printing
- github.go - Calling REST APIs
- sha1.go - Working with
io.Reader
&io.Writer
Links
- HTTP status cats
- errors package (Go 1.13)
- encoding/json
- net/http
- Let's talk about logging by Dave Cheney
- Numbers
- math/big - Big numbers
- Numeric types
- Strings
- Unicode table
- strings package - string utilities
- Go strings
- Annotated "Hello World"
- Effective Go - Read this!
- Go standard library - official documentation
- A Tour of Go
- Setting Up
- The Go SDK
- git
- IDE's: Visual Studio Code + Go extension or Goland (paid)
Data & Other
G☺
♡
- http.log.gz
Day 2
Agenda
- Sorting
- Working with slices
- Writing methods
- Understanding interfaces
- Catching panics
- The built-in recover function
- Named return values
- Processing text
- Reading line by line with bufio.Scanner
- Using regular expressions
- Working with maps
Code
- slices.go - Working with slices
- game.go - Structs, methods & interfaces
- empty.go - The empty interface, type assertions
- div.go - Catching panics
- freq.go - Most common word (files, regular expressions, maps)
Exercises
- Read and understand the sort package examples
- Implement
sortByDistance(players []Player, x, y int)
ingame.go
- Change
mostCommon
to return the most commonn
words (e.g.func mostCommon(r io.Reader, n int) ([]string, error)
)
Links
- regex101 - Regular expression builder
- Go Proverbs - Think about them ☺
- sort examples - Read and try to understand
- When to use generics
- Generics tutorial
- Methods, interfaces & embedded types in Go
- Methods & Interfaces in the Go tour
- Slices
- Slices & Slice internals on the Go blog
- Slice tricks
- Error Handling
- Defer, Panic and Recover
- errors package (Go 1.13)
- pkg/errors
Data & Other
Day 3
Agenda
- Distributing work
- Using goroutines & channels
- Using the sync package to coordinate work
- Timeouts & cancellation
- Working with multiple channels using select
- Using context for timeouts & cancellations
- Standard library support for context
Code
- go_chan.go - Goroutines & channels
- sleep_sort.sh - Sleep sort in bash
- taxi_check.go - Turn sequential code to parallel
- sites_time.go - Using sync.WaitGroup
- payment.go - Using sync.Once & sync.WaitGroup
- counter.go - Using the race detector, sync.Mutex and sync/atomic
- select.go - Using
select
- rtb.go - Using
context
for cancellations
Exercise
In taxi_check.go
- Limit the number of goroutines to "n". Which "n" yields the best results?
- Cancel all goroutines once there's an error or mismatch in signature
Links
- The race detector
- Uber Go Style Guide
- errgroup
- Data Race Patterns in Go
- Go Concurrency Patterns: Pipelines and cancellation
- Go Concurrency Patterns: Context
- Curious Channels
- The Behavior of Channels
- Channel Semantics
- Why are there nil channels in Go?
- Amdahl's Law - Limits of concurrency
- Computer Latency at Human Scale
- Concurrency is not Parallelism by Rob Pike
- Scheduling in Go
Data & Other
Day 4
Agenda
- Testing your code
- Working with the testing package
- Using testify
- Managing dependencies with go mod
- Structuring your code
- Writing sub-packages
- Writing an HTTP server
- Writing handlers
- Using gorilla/mux for routing Adding metrics & logging
- Using expvar for metrics
- Using the log package and a look at user/zap
- Configuration patterns
- Reading environment variables and a look at external packages
- Using the flag package for command line processing
Code
nlp
project
├── go.mod - Project & dependencies ├── nlp.go - Package code ├── doc.go - Package level documentation ├── nlp_test.go - Test & benchmark file ├── example_test.go - Testable example ├── stemmer - Sub module │ ├── stemmer.go │ └── stemmer_test.go ├── testdata - Test data │ └── tokenize_cases.toml - Test cases └── cmd - Executables └── nlpd - HTTP server ├── main.go └── main_test.go
Links
- Configuration
- Logging
- Metrics
- Built-in expvar
- Open Telemetry
- Prometheus
- Go Code Review Comments
- Tutorial: Getting started with multi-module workspaces
- Example Project Structure
- How to Write Go Code
- Documentation
- Godoc: documenting Go code
- Testable examples in Go
- Go documentation tricks
- gob/doc.go of the
gob
package. Generates this documentation go install golang.org/x/pkgsite/cmd/pkgsite@9ffe8b928e4fbd3ff7dcf984254629a47f8b6e63
(require go 1.18)pkgsite -http=:8080
(open browser on http://localhost:8080/${module name})
- Out Software Dependency Problem - Good read on dependencies by Russ Cox
- Linters (static analysis)
- staticcheck
- gosec - Security oriented
- golang.org/x/tools/go/analysis - Helpers to write analysis tools (see example)
- Testing
- testing
- testify - Many test utilities (including suites & mocking)
- Tutorial: Getting started with fuzzing
- testing/quick - Initial fuzzing library
- test containers
- HTTP Servers
- net/http
- net/http/httptest
- gorilla/mux - HTTP router with more frills
- chi - A nice web framework
Data & Other
- nlp.go
- stemmer.go
- tokenize_cases.toml
github.com/BurntSushi/toml