Taking the address of literals

September 28, 2023

Composite literals

Taking the address of a composite literal generates a pointer to a unique variable initialized with the literal’s value.

var pointer *Point3D = &Point3D{y: 1000}

This particular feature of the language is immensely useful.

Without it, the above code snippet would become much longer:

var point Point3D = Point3D{y: 1000}
var pointer *Point3D = &point

In fact, this is the exact situation we are in for non-composites. For reasons that confound me.

var intPointer = &int(3) // Invalid

var i = int(3)
var intPointer = &i // valid 🤷

This particular missing feature made it to my 10 things I hate about Go video.

It’s also annoyed Rob Pike, apparently, because back in 2021, he proposed a language change to solve this problem. But I’m still here waiting…

Quotes from The Go Programming Language Specification Version of August 2, 2023

Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .