When it comes to struct values in Go, one of the people’s common points of tension is whether to pass those around via pointers or just copy t

The Two Reasons I Prefer Passing Struct Pointers Around

submited by
Style Pass
2024-04-23 11:00:05

When it comes to struct values in Go, one of the people’s common points of tension is whether to pass those around via pointers or just copy the value. Because pointers come with some overhead, the natural reaction is to avoid them at all costs and resort to passing struct value copies wherever possible.

I am here to challenge that thought a little. Too much of the discourse around whether to use a certain Go construct becomes deeply technical too quickly. This may be justified when developing low-level system components with zero friction or massively scalable Web services. 

Neither is the case for most people. Suffice it to say, the chances of you giving up on your idea or your business going bankrupt are way higher than you ever need to worry about the performance of your Go app. When building regular software applications, I think it is far more important to focus on growing a codebase that is easy to build upon and maintain by multiple people over time. Therefore, rather than performance, the two reasons I would usually choose using pointer structs are identity and consistency.

One of my first clashes with reality as a software engineer was when I came to terms with the fact that real software engineering has much more to do with making the code easy to follow and modify than simply making the computer happy. Every feature a language offers comes with a cognitive burden and has trade-offs. Even in the case of a language as simple as Go, there are many places where a programming decision must be justified:

Leave a Comment