Overview
My work through the Programming with Google Go specialization from the University of California, Irvine on Coursera: twelve standalone Go programs, about 600 lines in total, each a self-contained package main that exercises one part of the language. Go has since become one of my working languages for middleware and services, and these exercises are where the concurrency model clicked.
What's in the repo
Language fundamentals
slice.go,trunc.go,findian.go: slices, type conversion and string handling on user input.makejson.go: building a map and marshalling it withencoding/json.read.go: reading a file line by line withbufiointo a slice of structs.bubblesort.go: an in-place bubble sort driven by a swap helper.kinematics.go: a closure,GenDisplaceFn, that captures acceleration, initial velocity and initial displacement and returns a function of time.
Types and interfaces
animals.go,zoo.go: anAnimalinterface (Eat,Move,Speak) implemented byCow,BirdandSnake, with an interactive prompt that creates and queries animals by name. This is the exercise that makes Go's implicit interface satisfaction concrete.
Concurrency
goroutines.go: splits an array into four chunks, sorts each in its own goroutine, waits on async.WaitGroupand merges the sorted results.race_conditions.go: two goroutines sharing state without synchronisation, written to demonstrate a race and whygo run -raceexists.dining_philosophers.go: five philosophers and five chopsticks modelled assync.Mutexvalues, each philosopher eating three times, coordinated with aWaitGroup.
Notes on the dining philosophers
The course version has every philosopher pick up the left chopstick then the right. That symmetric ordering can deadlock in principle if all five grab their left chopstick at once. The standard fixes are to impose a global lock ordering (always take the lower-numbered chopstick first) or to add a host goroutine that admits at most four philosophers at a time. If I revisited it, I'd implement the host with a buffered channel as a semaphore, which is the more idiomatic Go answer.
Built with
- Go standard library:
sync(Mutex,WaitGroup),encoding/json,bufio,os,fmt