Day 30: Reading Go — enough to navigate K8s/Docker/Terraform source
Why read Go even if you don't write it daily
Docker, Kubernetes, Terraform, and Prometheus are all written in Go. You don't need to become a Go developer — you need to be able to open their source, skim a function, and understand roughly what it does when debugging something unusual.
func (r *ReconcilerBase) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var pod corev1.Pod
if err := r.Get(ctx, req.NamespacedName, &pod); err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
// ... reconcile logic ...
return ctrl.Result{}, nil
}- A function returning
(value, error)— Go has no exceptions; errors are just returned values you must check explicitly if err != nil { return err }— the single most common line of Go code you will ever see- A
structwith a method attached (func (r *ReconcilerBase) ...) — Go's version of a class method - Interfaces are implicit — a type satisfies an interface just by having the right methods, no explicit "implements" keyword
Payoff, later
The Reconcile function shape above is exactly the pattern you'll write yourself in Phase 29 when building a tiny Kubernetes operator — recognizing it now means it won't be unfamiliar syntax fighting for your attention then.
Key terms
- error as a value
- Go's convention of returning errors explicitly rather than throwing exceptions.
- Implicit interface
- A Go type automatically satisfies an interface by having matching method signatures — no explicit declaration needed.
In Go, what does `if err != nil { return err }` immediately after a function call tell you?