/practical-go

This repo contains the material for the Practical Go class.

Primary LanguageGoApache License 2.0Apache-2.0

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:

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 (also GOARCH)
  • banner.go - Strings & printing
  • github.go - Calling REST APIs
  • sha1.go - Working with io.Reader & io.Writer

Terminal Log

Links

Data & Other


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)

Terminal Log

Exercises

  • Read and understand the sort package examples
  • Implement sortByDistance(players []Player, x, y int) in game.go
  • Change mostCommon to return the most common n words (e.g. func mostCommon(r io.Reader, n int) ([]string, error))

Links

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

Terminal Log

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

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

Terminal Log

Links

Data & Other