rmoff's random ramblings
about talks

Learning Golang (some rough notes) - S01E01 - Pointers

Published Jun 25, 2020 by in Go, Golang, Pointers at https://preview.rmoff.net/2020/06/25/learning-golang-some-rough-notes-s01e01-pointers/

Note
Learning Go : Background

šŸ‘‰ A Tour of Go : Pointers

I’ve never used pointers before. Found plenty of good resources about what they are, e.g.

  • https://www.callicoder.com/golang-pointers/

  • https://dave.cheney.net/2017/04/26/understand-go-pointers-in-less-than-800-words-or-your-money-back

But why? It’s like explaining patiently to someone that 2+2 = 4, without really explaining why would we want to add two numbers together in the first place.

(side note: I bet a ton of devrel material could be made more accessible by addressing the huge number of assumptions that are made, explicit and implicit, in explanations given)

My colleague Ricardo FerreiraĀ gave me this great explanation:

Pointers in Go has to do with three main things:

  1. Garbage Collection

    Go is a garbage collected language just like Java. However, it uses of some less sophisticated algorithms to reclaim memory space than Java that tries to figure out too much doing heap transverse and thus spending too much CPU on it. Go is meant to provide better performance and because of this it tries to share the responsibility with the developer about how to track references. And if a developer uses a pointer; it is the indication about which references need to be reclaimed given its scope. It uses something called reference counting to figure that out.

  2. Encapsulation (Information Hiding)

    Pointers are particularly good for structs, which represents complex data structures. By returning structs rather than the actual value developers can ensure that only the function that created the struct can act upon it.

  3. Immutability

    You better than anyone will understand this. Structs represents records and therefore their occurance needs to be immutable. You can’t change what happened in the past and thus; you are not supposed to change structs — unless the function that created the struct provides a write operation. So in Go, we use the concept of interface methods that are nothing more than function pointers to a struct.

I also got some useful feedback from people on Twitter:

Pointers in Go.

I grok *what* they are and *how* to use them (https://t.co/0nMV16r0Wz sorted me out there) - but can a kind soul point me to a good resource that explains *why* I would use them? Why don't I just pass variables around instead?

— Robin Moffatt šŸ»šŸƒšŸ„“ (@rmoff) June 24, 2020

šŸ“ŗ More Episodes… šŸ”—

  • Kafka and Go

    • S02E00 - Kafka and Go

    • S02E01 - My First Kafka Go Producer

    • S02E02 - Adding error handling to the Producer

    • S02E03 - Kafka Go Consumer (Channel-based)

    • S02E04 - Kafka Go Consumer (Function-based)

    • S02E05 - Kafka Go AdminClient

    • S02E06 - Putting the Producer in a function and handling errors in a Go routine

    • S02E07 - Splitting Go code into separate source files and building a binary executable

    • S02E08 - Checking Kafka advertised.listeners with Go

    • S02E09 - Processing chunked responses before EOF is reached

  • Learning Go

    • S01E00 - Background

    • S01E01 - Pointers

    • S01E02 - Slices

    • S01E03 - Maps

    • S01E04 - Function Closures

    • S01E05 - Interfaces

    • S01E06 - Errors

    • S01E07 - Readers

    • S01E08 - Images

    • S01E09 - Concurrency (Channels, Goroutines)

    • S01E10 - Concurrency (Web Crawler)


Robin Moffatt

Robin Moffatt works on the DevRel team at Confluent. He likes writing about himself in the third person, eating good breakfasts, and drinking good beer.

Story logo

© 2025