Live data from Hacker News

What I'd like to see in Go 2.0

sethvargo.com

211–220 of 223 posts

Re: What I'd like to see in Go 2.0

#211

I would really love to see default parameters and struct values. I jump between Python and Go in my day job and Go is a much better language overall but things like this make it painful

Default parameters have always struck me as dangerous. (And for my day job I use Python.)

I always hear this argument but I’ve never seen it be dangerous in my day to day. We deal with defaults all the time in programming and it’s generally fine

Re: What I'd like to see in Go 2.0

#212
post #179

Earlier quoted context omitted.

Go is focused on the distributed systems domain though. It's fine, even desirable, to have languages focused on particular domains, that make design decisions based on the constraints of the domain. In this domain, closed enums are footguns with costly consequences if you get it wrong.

As someone that doesn't work in that domain, could you give a short example?

You push Thrift clients out into the world expecting to a certain API field to be typed according to a 3-element enum. You add a 4th element to support a new feature in a new client. If you ever accidentally serve this 4th element to an old client, it will crash on deserialization. Bonus points if the client is old enough that it's not part of your testing regime anymore.

Re: What I'd like to see in Go 2.0

#213

Earlier quoted context omitted.

What problems did an enum cause you, and how was the enum responsible for the problem?

This is a known issue with Rust, for example. I have an enum with variants A and B. Somebody writes an exhaustive switch (match statement) that handles A and B with no default case. I add a variant C. Their code breaks because they don’t handle C. Adding an enum variant was a breaking change. In Rust, the answer is #[non_exhaustive], which forces consumers to always add a default case. It’s not a huge deal, just a kn…

If your package updates and users of your package update and their code ceases to compile, that seems... fine? It's the system working as intended. They can just downgrade back to the previous known good version. It would be much worse if you made a breaking change to code but consumers' code that used yours continued to compile but no longer functioned as expected

Re: What I'd like to see in Go 2.0

#214
post #129

Earlier quoted context omitted.

> > user code could simulate non-deterministic > I'm curious how? if rand.Intn(2) == 0 { select { case: chan3_whichItreatTheSameAsChan2 } } else { select { case: 0xFF ->chan3_whichItreatTheSameAsChan2 // a higher priority case: Yes, it increases verbosity to the other way, but no performance loss.

How in the world is generating a random number and branching and doubling the number of instructions "no performance loss"?

Now the non-deterministic implementation does more work than a deterministic implementation. It generates a random number and sorts the branches. The latter (sorts the branches) is not needed in the above pseudo code.

Doubling the number of instructions has no impact on run-time performance.

And there are more optimization opportunities in implementing a deterministic design. Now, the non-deterministic implementation needs to lock all involved channels before subsequent handling, a deterministic implementation might not need to.

Re: What I'd like to see in Go 2.0

#215
post #123

Earlier quoted context omitted.

Yes, as I have mentioned, there is performance loss, comparing to select { case: chan3_whichItreatTheSameAsChan2 }

The real usecases where I need deterministic select, are so few that a small performance loss doesn't matter to me.

Sometimes, it is not related to performance loss, it is related to implementation cleanness and complexity.

Re: What I'd like to see in Go 2.0

#216
post #123

Earlier quoted context omitted.

Yes, as I have mentioned, there is performance loss, comparing to select { case: chan3_whichItreatTheSameAsChan2 }

1) Is there really a performance loss compared to if select was deterministic ? 2) What in the world do you need such code for?

1) surely.

2) just read:

     https://groups.google.com/g/golang-nuts/c/SXsgdpRK-mE/m/CT7UjJ3aBAAJ

     https://groups.google.com/g/golang-nuts/c/ZrVIhHCrR9o

     https://groups.google.com/g/golang-nuts/c/lEKehHH7kZY/m/SRmCtXDZAAAJ

Re: What I'd like to see in Go 2.0

#217
My biggest annoyance so far is the inability (and inconsistency) to have a one-liner to get a pointer to something else than a struct.

I can write x = &Foo{...} but somehow x = &42 and x = &foo() are not allowed, which forces me in some cases to declare useless variables that hurts readability.

Re: What I'd like to see in Go 2.0

#218

Earlier quoted context omitted.

Easily fixed using lexical scopes: func doThing() string { result := "OK" { var err error if result, err = somethingElse(); err != nil { return "ERROR" } } return result } `err` is introduced in the lexical scope, `result` isn't so it still refers to the string from the surrounding scope. `err` does not pollute the surrounding scope. You can also try the complete version here: https://go.dev/play/p/kDEB11YdvSs

In my experience this isn’t idiomatic Go and would definitely turn heads in a code review. But in such a small function, it’s fine to just not worry about the lifetime of your variables (and in bigger functions you can often decompose into smaller functions).

I agree. The idiomatic thing here would be to simply declare `var err error` in the surrounding scope, especially since this example is a small function where this wouldn't matter.

But "idiomatic" always takes a backseat compared to technical necessity. If there is a requirement, for some reason, to limit the scope of `err` and still allow `result` to be changed in the if's pre-assignment, then this is the way to do it.

Re: What I'd like to see in Go 2.0

#219
post #215

Earlier quoted context omitted.

The real usecases where I need deterministic select, are so few that a small performance loss doesn't matter to me.

Sometimes, it is not related to performance loss, it is related to implementation cleanness and complexity.

A separate `select` with empty `default` is about as simple and clean as it gets. It is easy to read, easy to reason about, and, most importantly, conveys the intention of the code perfectly.

Re: What I'd like to see in Go 2.0

#220

Two Go 2 proposals that interested me were: * nillability annotations: https://github.com/golang/go/issues/49202 * Change int from a machine word size (int32 or int64) to arbitrary precision (bigint): https://github.com/golang/go/issues/19623 Sadly the nillability annotations were rejected because they weren't backwards compatible. The bigint change is also unlikely to be accepted because the issue is already five ye…

If this was implemented:

    func foo(a, b int) int {
        return a * b
    }
At compile time, what is the return type of foo? Answer: Unknown. The compiler has no way of determining if the resulting type will be small or big int. This has to be taken into account not just in foo, but in every function calling foo, and every function that is called with the return of foo as a param.

One of the best things about golang, is how little "magic" there is. Everything that happens is immediately obvious from the code. An int that is upgraded to a completely different type when it reaches certain values, goes directly counter to that; it would be hidden behaviour, not immediately obvious from the code.

Should golang have a builtin arbitrary sized integer type? Maybe. Using math/big can become cumbersome. But a much better solution would be to introduce a new builtin type like `num`

Post reply on HN