Live data from Hacker News

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

jvns.ca

31–40 of 176 posts

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

#31
> though apparently structs will be automatically copied on assignment if the struct implements the Copy trait

What's actually going on is that the Rust compiler is always allowed to choose whether to just copy bits during assignment, but if your type implements Copy then the value isn't gone after it has been assigned as it would be with the ordinary destructive move assignment semantic -- so any code can continue to use the value whereas otherwise it would no longer exist having been moved.

Some languages make the built-in types magic and you can't have that magic in your own types, Rust mostly resists this, a few things are magic in the stdlib and you wouldn't be allowed to do this magic in stable Rust (it's available to you in nightly) but mostly, as with Copy, your types are just as good as the built-in types.

This actually feels really natural after not long in my experience.

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

#32
post #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 dem…

Non-C# developer question: what use-case/situation would a `struct` make sense to use instead of a `class`? Just out of curiosity.

[Edit] Well, there's a nice, special article for this very question: https://learn.microsoft.com/en-us/dotnet/standard/design-gui...

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

#34
post #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 dem…

Non-C# developer question: what use-case/situation would a `struct` make sense to use instead of a `class`? Just out of curiosity. [Edit] Well, there's a nice, special article for this very question: https://learn.microsoft.com/en-us/dotnet/standard/design-gui...

usually performance reasons; don't want to allocate on the heap, contributing to GC pressure, or you want pass by copy semantics

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

#36
post #29

The one about named returns, err always being nil, why is err even in scope, seems like it should be a compile error to me? (I rarely write Go).

In the function signature, the return variable err is type error. Since it is named it is also defined and initialized as nil.

In the code, an error is found and it does not assign a value to err and just returns it as the error value.

So it returns nil as the error when it wants to return an error with a proper value.

The code should be something like:

    if ctx.Err() != nil {
      return 0, 0, ctx.Err()
    }

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

#37
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…

most of the time when people don't ask questions in industry, it isn't because they already know the answers, it's because they don't care what the answers are

blogging is culturally different because there's way more exhibitionism involved

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

#38
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 copying huge blocks of data free in 2024? My benchmarks suggest otherwise, and the world still needs assembly programmers.

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

#39
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…

This is what jumped out to me reading this article too - unashamedly admitting to having gaps in knowledge that some others might take for granted.

Bucketing some of those gaps as “probably useful but not to me right now” is also great. It shows a purpose to focus on what matters right now, with a hint to return to when it does pop up again later.

Lots of folks I work with and respect sometimes get trapped in the weeds having to understand every little thing. It’s good to be curious, but sometimes filing it away for later and shipping is more important now.

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

#40
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.

Yeah I'm fairly new to Go, but have done plenty of work in languages with "no pointers" (Python, Java), with pointers (Objective-C), and with "reference/value types" (Swift). I thought I was pretty proficient with all of this, but Go's implementation was really unergonomic. It feels like it's trying to be C, but treating it more like Swift has been more effective I've found. How this all interacts with nils and missing fields in structs is also not very ergonomic, and it feels like the language is obviously missing an optional type, yet I've not seen one used commonly yet.
Post reply on HN