Golang for range Loops in 5 Minutes
Go for range loops for beginners, in five minutes, on a real Go program: why changing the value in a range loop leaves the slice unchanged, writing through the index instead, and why a map prints in a different order every run until you sort its keys with slices.Sorted(maps.Keys(m)).
The loop that changed nothing
You wrote a for range loop in Go to change every item in a list. It ran, and the list didn't change. Joe weighs each fish on his charter boat in a bucket that itself weighs 300 grams, so every weight in his catch log is 300 grams too high. He writes a loop that takes 300 off each catch's Grams, its weight.
sed -n '19,23p' main.go// take off the 300 g bucket for _, c := range catches { c.Grams -= 300 fmt.Println("loop:", c.Fish, c.Grams) }
Inside the loop, the mackerel drops to 1200. Then he prints the log, and it still says 1500. The loop did its job. It just did it to something else. So where did the 300 grams go?
go run .loop: mackerel 1200 loop: sea bass 2100 loop: cod 3800 loop: mackerel 800 loop: pollock 2900 -- log -- mackerel 1500 sea bass 2400 cod 4100 mackerel 1100 pollock 3200
What range hands you
To find them, start with what range really does. Picture the harbour office. The clerk reads the logbook out to you, one line at a time, and you copy each line onto your own notepad. Cross a number out on your notepad, and the logbook doesn't change. That's what range does.

Joe's catch log is a slice, Go's word for a list. Each time round, a for range loop over it hands you two things: the position, called the index, and the value at that position. Joe prints both. The first line reads zero, mackerel, 1500, and the last, four, pollock. Go counts positions from zero. His first loop didn't need the index, so he wrote an underscore in its place, which tells Go to throw it away.
sed -n '19,21p' main.gofor i, c := range catches { fmt.Println(i, c.Fish, c.Grams) }
go run .0 mackerel 1500 1 sea bass 2400 2 cod 4100 3 mackerel 1100 4 pollock 3200
Here's where the 300 grams went. That value, c, isn't the catch in the log. Each round, Go puts a fresh copy of the next entry into c. Joe's line changed the copy, like a number crossed out on your notepad. Next round, c is overwritten with the next catch, and the log stays untouched.

Change the log, not a copy
To change the log itself, Joe uses the index. Leave out c, and range hands you only the position, which Joe calls i. Then catches.Grams reaches into the log at that position and changes the real entry.

Now every catch in the log is 300 grams lighter: the mackerel reads 1200, and the pollock 2900.
go run .-- log -- mackerel 1200 sea bass 2100 cod 3800 mackerel 800 pollock 2900
Range over a number
Joe runs three trips a day. He has no list, just a number. How do you loop exactly three times? Since Go 1.22, range can take a plain number. A number has no items, so range gives just the position: zero, one, two. Joe adds one to print trip 1, 2 and 3.
sed -n '6,8p' main.gofor trip := range 3 { fmt.Println("trip", trip+1, "of 3") }
go run .trip 1 of 3 trip 2 of 3 trip 3 of 3
Ranging over a map
Jess, who sells Joe's catch at the market, wants the total for each kind of fish. Joe adds them up in a map, a lookup from fish name to grams, and ranges over it. On a map, range hands you two things: the fish name, called the key, and its grams, called the value. Then he runs it three times in a row. Guess first: the same order every run? It isn't. Same program, same fish, two different orders.
sed -n '23,30p' main.gototals := map[string]int{} for _, c := range catches { totals[c.Fish] += c.Grams } for fish, g := range totals { fmt.Print(fish, " ", g, " ") }
for n in 1 2 3; do go run .; donemackerel 2000 sea bass 2100 cod 3800 pollock 2900 mackerel 2000 sea bass 2100 cod 3800 pollock 2900 sea bass 2100 cod 3800 pollock 2900 mackerel 2000
That's on purpose. The Go spec says it plainly: map order isn't specified, and it can change from one loop to the next. Go even starts each loop at a random spot, so no program comes to rely on an order.

When order matters, sort the keys first, with two helpers from Go's standard library. maps.Keys gives Joe the fish names. slices.Sorted puts them in alphabetical order. Then he ranges over that sorted list, looking up each total. Three runs, one order: cod, mackerel, pollock, sea bass.
sed -n '31,33p' main.gofor _, fish := range slices.Sorted(maps.Keys(totals)) { fmt.Print(fish, " ", totals[fish], " ") }
for n in 1 2 3; do go run .; donecod 3800 mackerel 2000 pollock 2900 sea bass 2100 cod 3800 mackerel 2000 pollock 2900 sea bass 2100 cod 3800 mackerel 2000 pollock 2900 sea bass 2100
Recap
So: range hands you a position and a copy. To change the list, write through the index. Range over a number counts from zero. And a map has no order, so sort the keys when it matters.

If range loops make sense now, a like helps YouTube show this to more beginners, and subscribing brings the next Go idiom: make, for slices and maps.
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 charter boat and its catches are made up. Every command, output and number was real. Watch the full video on YouTube.