When I first started learning Go, I thought I was doing everything rightβuntil I ran into a weird bug about golang range loop reference. I was iterating over a list of Book structs (of course, I can't share the real structs and code used here... all here are for turorial purpose), taking the pointer of each one, and storing them into a slice. But at the end of the loop, all the pointers pointed to... the same book?! π€― Letβs walk through this classic Go beginner mistake together β and fix it the right way. π The Use Case: A Slice of Books in a Library Suppose we have a list of books, and we want to collect pointers to each one so we can modify them later. Hereβs the code I thought would work: for _, book := range books { bookPointers = append(bookPointers, &book) // Oops... } But when I printed out the pointers, they all pointed to the last book in the list. This bug stumped me for a while until I understood one critical Go behavior. The File Structure To Run The Code learning-golang/ βββ 01-loop-reference-pitfall/ β βββ main.go β βββ README.md βββ Makefile βββ bin/ βββ go.mod This is the complete buggy code: package main import ( "fmt" ) type Book struct { Title string Author string } func main() { originalBooks := []Book{ {"Go in Action", "William Kennedy"}, {"The Go Programming Language", "Alan Donovan"}, {"Introducing Go", "Caleb Doxsey"}, } fmt.Println("β Buggy Version:") var buggyPointers []*Book for _, book := range originalBooks { buggyPointers =…