Lesson 26. A few pointers
After reading lesson 26, you’ll be able to
- Declare and use pointers
- Understand the relationship between pointers and random access memory (RAM)
- Know when to use—and not use—pointers
Walk around any neighborhood and you’ll likely encounter homes with individual addresses and street signs to guide you on your way. You may happen upon a closed-down shop with an apologetic sign: “Sorry, we’ve moved!” Pointers are a bit like the sign in the store window that directs you to a different address.
A pointer is a variable that points to the address of another variable. In computer science, pointers are a form of indirection, and indirection can be a powerful tool.
All problems in computer science can be solved by another level of indirection...
David Wheeler
Pointers are quite useful, but over the years they’ve been associated with a great deal of angst. Languages in the past—C in particular—had little emphasis on safety. Many crashes and security vulnerabilities can be tied back to the misuse of pointers. This gave rise to several languages that don’t expose pointers to programmers.
Go does have pointers, but with an emphasis on memory safety. Go isn’t plagued with issues like dangling pointers. This would be like heading to the address for your favorite shop, only to find it was accidentally replaced with the parking lot for a new casino.