Live data from Hacker News

Go structs are copied on assignment (and other things about Go I'd missed)

jvns.ca

21–30 of 176 posts

Re: Go structs are copied on assignment (and other things about Go I'd missed)

#21
post #3

Not understanding structs vs pointers is a pretty basic misconception in go. Does this trip anyone else up? I found it unenlightening / unsurprising, and the linked "100 mistakes" piece also very basic and in some cases just plain wrong.

As she points out though, a lot of dynamic languages don’t behave this way. A string after all points to a heap allocation; so it’s not unreasonable to think of a string as a pointer.

In those languages, nearly everything is a pointer, they just don't call it that, which causes unnecessary confusion when you need to understand what's really happening. (E.g., in Java, a String is a pointer to a string, not a string; but an int is just an int, not a pointer to an int.)

Re: Go structs are copied on assignment (and other things about Go I'd missed)

#22
post #10

One of the many things I find inspiring about Julia is how quick she is to admit to mistakes she has made or things that she hasn't understood. If she didn't understand it, I can 100% guarantee that there are large numbers of people out there who also didn't understand it - many of whom were probably too embarrassed to ever admit it. I think this is a useful trait for senior software engineers generally. If you're a…

Personally, I think that the idea that programmers should know everything is kinda bizarre.

Programming is about finding the answer. If you already knew everything, you could just sit down and type any program from your knowledge.

We all know that you can't know everything otherwise, why even write documentation or use git?

Unfortunately, it's difficult to move past this idea, and it's so pervasive that stating "I don't know" can negatively affect your standing for some.

Re: Go structs are copied on assignment (and other things about Go I'd missed)

#24
post #22
post #10

One of the many things I find inspiring about Julia is how quick she is to admit to mistakes she has made or things that she hasn't understood. If she didn't understand it, I can 100% guarantee that there are large numbers of people out there who also didn't understand it - many of whom were probably too embarrassed to ever admit it. I think this is a useful trait for senior software engineers generally. If you're a…

Personally, I think that the idea that programmers should know everything is kinda bizarre. Programming is about finding the answer. If you already knew everything, you could just sit down and type any program from your knowledge. We all know that you can't know everything otherwise, why even write documentation or use git? Unfortunately, it's difficult to move past this idea, and it's so pervasive that stating "I do…

Right! The best thing about our chosen career is that it's completely impossible to know everything - there's always a new corner of software engineering to dig into, be it how the Linux kernel works, or programming with Haskell, understanding Transformer LLM architectures, or how the Svelte compiler works or whatever.

Re: Go structs are copied on assignment (and other things about Go I'd missed)

#25

To generalize the title into a rule is good to remember that in Go everything is passed by value(copy).

Maps and channels and functions are passed by reference. Slices are passed and returned by value but sometimes share state invisibly, the worst of both worlds. It would make more sense if Go either made this stuff immutable, made defensive copies, or refused and required using explicit pointers for all these cases.

Re: Go structs are copied on assignment (and other things about Go I'd missed)

#27
post #3

Not understanding structs vs pointers is a pretty basic misconception in go. Does this trip anyone else up? I found it unenlightening / unsurprising, and the linked "100 mistakes" piece also very basic and in some cases just plain wrong.

[dead]

Re: Go structs are copied on assignment (and other things about Go I'd missed)

#28
This sometimes catches out people C#/.Net too, it's a big difference between Class and Struct, Class is reference type and Struct is value type. (see fiddle below), but in practice people very rarely reach for structs, so people don't tend to build up the muscle memory of using them, even if they intuitively understand the difference between reference types and value types from general use of other types.

(Fiddle demonstration for non-.Net peeps: https://dotnetfiddle.net/apDZP5 ).

Re: Go structs are copied on assignment (and other things about Go I'd missed)

#30
post #15

To generalize the title into a rule is good to remember that in Go everything is passed by value(copy).

That is the case for almost every modern language. C++ is one of the few languages that has "references" and at least last I looked that's a language accommodation over what are pointers being passed by value in the assembly, at least until compiler optimizations take over (and that's not limited to references either). If you're in 2024 and you're in some programming class making a big deal about pass-by-value versus…

Is python no longer a modern language? Objects are certainly not copied when passed to a function.
Post reply on HN