Golang context.Context in 5 Minutes
Go's context.Context for beginners, in five minutes, on a real Go web server: why a server keeps working after the customer leaves, passing r.Context() down to an outgoing request, context.WithTimeout with defer cancel, why your own loops must select on ctx.Done(), the context package's rules, and go vet's lost-cancel check.
6 seconds of work for nobody
Your Go server keeps working for customers who already left: here, six seconds after one gave up.

Jen asks John's garden center if the ferns are in stock, and gives up after two seconds. Here, curl plays Jen, and its max-time flag is her two-second patience. John's server keeps asking the plant supplier anyway, and logs the answer at eight seconds. That's six seconds after she left. The ferns, for the record, were in stock. Why didn't it stop? That's the job of context.Context, and in five minutes this server will stop the moment she leaves.
curl --max-time 2 'localhost:8080/stock?plant=fern'curl: (28) Operation timed out after 2006 milliseconds with 0 bytes received
go run .garden shop listening on :8080 fern: 12 in stock (8.0s) signal: interrupt
What a context is
Picture the garden center's yard. A clerk sends a runner to the far greenhouse to count ferns. If the runner carries a walkie-talkie, the clerk can radio: stop, she left. Without one, the runner counts every fern, and walks back to an empty counter.

In Go, the walkie-talkie is a context. It's a value you pass to every function that handles one request. It carries a cancel signal. It can also carry a deadline, if you set one. Go's built-in HTTP package makes one for every request. Every handler receives the incoming request, usually named r, and you get its context by calling r.Context(). When the customer disconnects, Go cancels it for you. Every function you passed it to can hear that, and stop.

Pass it down
John's function that calls the supplier, askSupplier, never got the radio. So he adds a context as its first parameter, called ctx by convention. Plain http.Get can't hear a context. So he builds the request with NewRequestWithContext. That attaches ctx to it. Then he sends it with http.DefaultClient.Do. And the handler passes r.Context() in.

Jen gives up at two seconds again. This time the log says: context canceled, at two point oh seconds. The request to the supplier stopped the moment she left.
curl --max-time 2 'localhost:8080/stock?plant=fern'curl: (28) Operation timed out after 2005 milliseconds with 0 bytes received
go run .garden shop listening on :8080 fern: Get "http://localhost:9000/fern": context canceled (2.0s) signal: interrupt
A deadline of your own
Canceling only helps when someone leaves. If Jen keeps waiting, she waits eight seconds. So John sets a limit of his own: context.WithTimeout, three seconds. It takes the request's context and makes a new one from it. That new one is canceled at three seconds, or when Jen leaves, whichever comes first. It also hands back a cancel function. Go's defer keyword runs it when the handler finishes. That way, the three-second timer doesn't keep running after Jen has her reply. Now, at three seconds, Jen gets a reply that says: supplier didn't answer. And the log shows why: not context canceled this time, but context deadline exceeded.
sed -n '30,32p' main.goctx, cancel := context.WithTimeout(r.Context(), 3*time.Second) defer cancel() answer, err := askSupplier(ctx, plant)
curl 'localhost:8080/stock?plant=fern'supplier didn't answer
go run .garden shop listening on :8080 fern: Get "http://localhost:9000/fern": context deadline exceeded (3.0s) signal: interrupt
Your code has to listen
What if the slow work is John's own code? John's server has a second address, care, that builds a care guide for a plant. It's five pages long, and each page takes one second. He passes ctx in, just like he did for askSupplier. Jen leaves at two and a half seconds. Guess first: when does the loop stop? At five seconds. All five pages. A context doesn't stop your code by itself. It's only a signal, and your code has to listen for it.
sed -n '43,46p' main.gofor page := 1; page <= 5; page++ { time.Sleep(time.Second) // slow work: one page of the care guide log.Printf("%s: care page %d", plant, page) }
curl --max-time 2.5 'localhost:8080/care?plant=fern'curl: (28) Operation timed out after 2505 milliseconds with 0 bytes received
go run .garden shop listening on :8080 fern: care page 1 fern: care page 2 fern: care page 3 fern: care page 4 fern: care page 5 fern: care sheet done (5.0s) signal: interrupt
So inside the loop, John uses select, which waits for whichever happens first. One option is the next page: a one-second timer from time.After. The other is ctx.Done(): that's the walkie-talkie, and it crackles the moment the context is canceled. If the context is done first, he returns its error. Now the log stops after page two: context canceled, at two and a half seconds.
sed -n '43,53p' main.gofor page := 1; page <= 5; page++ { select { case <-ctx.Done(): return ctx.Err() case <-time.After(time.Second): // slow work: one page } log.Printf("%s: care page %d", plant, page) } return nil }
curl --max-time 2.5 'localhost:8080/care?plant=fern'curl: (28) Operation timed out after 2506 milliseconds with 0 bytes received
go run .garden shop listening on :8080 fern: care page 1 fern: care page 2 fern: context canceled (2.5s) signal: interrupt
The rules
There are a few rules, and if you break them, the stop signal never reaches the code doing the work. They're in the context package's own docs. Every request gets a context. Every function from the handler down to your outgoing calls passes it along. When it's canceled, every context made from it is canceled too, like John's three-second one. Don't store it somewhere to reuse later: it belongs to one request only. Pass it as the first parameter, named ctx.

One rule is checked for you, by go vet, Go's built-in code checker. If John throws the cancel function away with an underscore, go vet stops him: the cancel function should be called, not discarded, to avoid a context leak. That's exactly the leak John's defer cancel line prevents.
sed -n '30p' main.goctx, _ := context.WithTimeout(r.Context(), 3*time.Second)
go vet .main.go:30:7: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak
Recap
So: Jen left, and John's server kept working. Passing ctx down let it stop, WithTimeout gave it a deadline, and select made his own loop listen.

If context makes sense now, a like helps YouTube show this to more beginners, and subscribing brings the next Go idiom: structs.
All the lab files and commands are linked in the description. Thanks for watching Cloud Explainers.
Sources
Demos: one container on our own Mac standing in for a laptop; the garden center and its supplier are made up. Every command, error and number was real. Watch the full video on YouTube.