GeekCoding101

  • Home
  • GenAI
    • Daily AI Insights
    • Machine Learning
    • Transformer
    • Azure AI
  • DevOps
    • Kubernetes
    • Terraform
  • Technology
    • Cybersecurity
    • Dev Tips
  • About
  • Contact
golang
Dev Tips

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 45hotness 0likes Geekcoding101 Read all
Newest Hotest Random
Newest Hotest Random
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 Mastering Terraform with AWS Guide Part 1: Launch Real AWS Infrastructure with VPC, IAM and EC2 ExternalName and LoadBalancer - Ultimate Kubernetes Tutorial Part 5 NodePort vs ClusterIP - Ultimate Kubernetes Tutorial Part 4
Mastering Terraform with AWS Guide Part 1: Launch Real AWS Infrastructure with VPC, IAM and EC2Terraform Meta Arguments Unlocked: Practical Patterns for Clean Infrastructure CodeTerraform Associate Exam: A Powerful Guide about How to Prepare ItGolang Range Loop Reference - Why Your Loop Keeps Giving You the Same Pointer (and How to Fix It)
OAuth 2.0 Authorization Code Flow Ultimate Kubernetes Tutorial Part 2: DNS server and NTP server Configuration What Is Prompt Engineering and How to "Train" AI with a Single Sentence? Terms Used in "Attention is All You Need" Master Gradient Descent and Binary Classification: Supervised Machine Learning – Day 9 Honored to Pass AI-102!
Newest comment
Tag aggregation
Machine Learning notes Transformer cybersecurity Supervised Machine Learning security Daily.AI.Insight AI

COPYRIGHT Β© 2024 GeekCoding101. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang