Go Concurrency, Testing & Deployment
Overview
This tutorial explores Go's powerful concurrency model with goroutines and channels, writing tests with the testing package, and building production binaries. You'll learn to write safe, concurrent code and deploy efficient Go applications.
1. Goroutines & Channels
Goroutines
A goroutine is a lightweight thread managed by the Go runtime.
Channels
Channels are the pipes that connect goroutines.
2. Concurrency Patterns
Worker Pool
Fan‑Out / Fan‑In
Fan‑out: multiple goroutines read from the same channel. Fan‑in: multiple goroutines write to the same channel.
Select Statement
3. Testing with the Testing Package
Go has a built‑in testing package.
Run tests: go test or go test -v
Table‑Driven Tests
4. Building & Deploying Go Binaries
Building
Deployment Options
- Binary: Upload the single binary to a server.
- Docker: Use a minimal base image (alpine).
- Cloud: Deploy to Google Cloud Run, AWS Lambda, or Heroku.
Dockerfile for Go
Quiz
Question 1
What is a goroutine?
- A lightweight thread managed by the Go runtime
- A heavy system process
- A synchronization primitive
- A database connection pool
Show answer
Question 2
Which statement is used to send a value to a channel?
- ch <- value
- value := <-ch
- ch.send(value)
- value == ch
Show answer
Question 3
Which command runs Go tests?
- go test
- go run test
- go build test
- go bench
Show answer
Exercises
Exercise 1
Write a program that starts 5 goroutines, each printing its ID. Use a sync.WaitGroup to wait for all to complete.
Sample answer
Exercise 2
Write a table‑driven test for a function that multiplies two integers.
Sample answer
Homework
Homework 1
Extend the Task Manager API from Tutorial 1 with: (1) A concurrent worker that periodically logs the number of tasks; (2) Unit tests for the handlers; (3) A Dockerfile for deployment.
Sample outline
- Worker: Use a goroutine with a ticker to log task count every 10 seconds.
- Tests: Write tests for GET, POST, PUT, DELETE using the testing package.
- Dockerfile: Multi‑stage build with alpine base.
- Build: go build -o task-api .
Mini‑Project
Concurrent URL Checker
Build a concurrent URL health checker:
- Accepts a list of URLs from a file or API request
- Checks each URL in parallel using goroutines
- Returns status (200 OK, 404 Not Found, etc.)
- Implements a timeout (5s per request)
- Writes tests for the checker function
- Deploy as a CLI tool or HTTP API
Sample implementation outline
- Function: checkURL(url string) (statusCode int, err error)
- Worker pool: Limit concurrent checks to 10 goroutines
- Channels: URL channel for input, result channel for output
- Timeout: Use context.WithTimeout
- Testing: Mock HTTP responses for tests
Tutorial Summary
You explored Go's powerful concurrency model with goroutines and channels, learned to write comprehensive tests with the testing package, and mastered building and deploying Go binaries. You now have the skills to build concurrent, testable, and production‑ready Go applications.
Key takeaway: Go's concurrency primitives are simple yet powerful. Combined with its fast compilation and single‑binary deployment, Go is an excellent choice for modern cloud‑native applications.