GeekCoding101

  • Home
  • GenAI
    • Daily AI Insights
    • Machine Learning
    • Transformer
    • Azure AI
  • DevOps
    • Kubernetes
    • Terraform
  • Tech
    • CyberSec
    • System Design
    • Coding Notes
  • About
  • Contact
golang
Coding Notes

Golang Range Loop Reference - Why Your Loop Keeps Giving You the Same Pointer (and How to Fix It)

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 =…

May 4, 2025 0comments 208hotness 0likes Geekcoding101 Read all
Newest Hotest Random
Newest Hotest Random
Secure by Design Part 1: STRIDE Threat Modeling Explained Kubernetes Control Plane Components Explained A 12 Factor Crash Course in Python: Build Clean, Scalable FastAPI Apps the Right Way Golang Range Loop Reference - Why Your Loop Keeps Giving You the Same Pointer (and How to Fix It) Terraform Associate Exam: A Powerful Guide about How to Prepare It Terraform Meta Arguments Unlocked: Practical Patterns for Clean Infrastructure Code
A 12 Factor Crash Course in Python: Build Clean, Scalable FastAPI Apps the Right WayKubernetes Control Plane Components ExplainedSecure by Design Part 1: STRIDE Threat Modeling Explained
A Tutorial of Angular, Karma and Jasmine Terms Used in "Attention is All You Need" Honored to Pass AI-102! A 12 Factor Crash Course in Python: Build Clean, Scalable FastAPI Apps the Right Way Build and Sign RPM package and repo Parameters vs. Inference Speed: Why Is Your Phone’s AI Model ‘Slimmer’ Than GPT-4?
Newest comment
Tag aggregation
notes security cybersecurity Supervised Machine Learning Daily.AI.Insight Transformer AI Machine Learning

COPYRIGHT © 2024 GeekCoding101. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang