News for hackers. Stuff that matters.
Re:Fixing for Loops in Go 1.22 by tazjin on Tuesday September 19, 2023 @05:35PM (#37577106)
Go has a long list of booby traps like this and prides itself on them. From outside of the Go team it looks like a small cultural shift might slowly be happening, cleaning up some of the obvious mistakes everyone's been telling them about since the beginning. Rob Pike retiring and giving up some formal power with that probably helps.
Re:Fixing for Loops in Go 1.22 by thiht on Tuesday September 19, 2023 @05:46PM (#37577265)
> Go has a long list of booby traps like this

Huh? Where’s the list? From the top of my head I think this is the only thing that repeatedly bit me, although I’m very aware of the behavior of for loop scoping. Linters save me nowadays at least.

Are there other things like that in the language that deserve a fix? Maybe things to do with json un/marshaling?

Re:Fixing for Loops in Go 1.22 by the_gipsy on Tuesday September 19, 2023 @05:50PM (#37577315)
My number one is not having algebraic/sum/union types leading to needing zero-values. Which is more like a never idling foot gatling gun.
Re:Fixing for Loops in Go 1.22 by wazzaps on Tuesday September 19, 2023 @05:56PM (#37577417)
Copying a mutex by value (thus duplicating the lock, causing deadlocks or worse) is far too easy
Re:Fixing for Loops in Go 1.22 by usefulcat on Tuesday September 19, 2023 @06:17PM (#37577718)
Wow, even c++ won't let you do that (without invoking undefined behavior, anyway).
Re:Fixing for Loops in Go 1.22 by icholy on Tuesday September 19, 2023 @05:57PM (#37577447)
`go vet` catches this.
Re:Fixing for Loops in Go 1.22 by erik_seaberg on Tuesday September 19, 2023 @06:42PM (#37578048)
Is there a way to declare any type uncopyable? This is something I always thought Ada got right.
Re:Fixing for Loops in Go 1.22 by smasher164 on Tuesday September 19, 2023 @06:54PM (#37578188)
you stick this in your struct

    type noCopy struct{}
    func (*noCopy) Lock()   {}
    func (*noCopy) Unlock() {}
Re:Fixing for Loops in Go 1.22 by mhh__ on Tuesday September 19, 2023 @06:52PM (#37578170)
I use a tool called "type theory"
Re:Fixing for Loops in Go 1.22 by icholy on Tuesday September 19, 2023 @07:11PM (#37578352)
Keep posting about D, I'm sure it will catch on soon.
Re:Fixing for Loops in Go 1.22 by nu11ptr on Tuesday September 19, 2023 @07:19PM (#37578412)
It has been a while, but yes, there were a lot of them and I forget most. It made it kinda pointless to me that the language was "easy" when the code felt so brittle (Null pointers...really?).

One weird thing that always goofed me up was that slices are passed by value but maps by reference. Always made it confusing how to pass them for serialization/deserialization. The compiler didn't complain it just panicked. Seemed like something the type system should catch.

Re:Fixing for Loops in Go 1.22 by skitter on Tuesday September 19, 2023 @07:21PM (#37578431)
If you append to a slice, you can't rely on the changes showing up in the original slice, nor on them not showing up.
Re:Fixing for Loops in Go 1.22 by kevincox on Tuesday September 19, 2023 @09:31PM (#37579398)
That the printf functions shit garbage into your output if you get the formatting specifiers wrong.

I'd much rather have a crash than silently corrupting output.

Re:Fixing for Loops in Go 1.22 by telotortium on Tuesday September 19, 2023 @11:39PM (#37580130)
[delayed]
Re:Fixing for Loops in Go 1.22 by rsc on Tuesday September 19, 2023 @05:49PM (#37577312)
Speaking as the person Rob Pike handed the formal power to (8 years ago now), I don't think that change has much to do with it.

We've known about the problem for a long time. I have notes from the run up to Go 1 (circa 2011) where we considered making this change, but it didn't seem like a huge problem, and we were concerned about breaking old code, so on balance it didn't seem worth it.

Two things moved the needle on this particular change:

1. A few years ago David Chase took the time to make the change in the compiler and inventory what it broke in a large code base (Google's, but any code base would have worked for that purpose). Seeing that real-world data made it clear that the problem was more serious than we realized and needed to be addressed. That is, it made clear that the positive side of the balance was heavier than we thought it was back in 2011.

2. The design of Go modules added a go version line, which we can key the change off. That completely avoids breaking any old code. That zeroed out the negative side of the balance.

Re:Fixing for Loops in Go 1.22 by orblivion on Tuesday September 19, 2023 @06:11PM (#37577629)
What about let's say 5 years from now, someone digs up a Go project from 2022, decides to get it up to speed for 2028, updates the version line. Is there something that would remind them to check for breaking changes, including this one? Perhaps the go project initializer could add a comment above the version line with a URL with a list of such changes. Though, that wouldn't help for this change.
Re:Fixing for Loops in Go 1.22 by flakes on Tuesday September 19, 2023 @06:57PM (#37578216)
I think the key difference here is to consider toleration vs adoption. Old code is able to tolerate the changes and still work in new ecosystems. There is still work on maintainers if they want to actually adopt the features themselves. Allowing these two concepts to work together is what allows iteratively updating the world, rather than requiring big bang introduction of features.

As for validating your software, the answer is the same as its always been… tests, tests and more tests.