Hacker News new | ask | show | jobs
by giveupitscrazy 1431 days ago
I know zero go, despite having worked with it professionally for a short stint that I try and block out of my memory (not go's fault), anyway that first example looks like gibberish to me while the second is super clear.
3 comments

FWIW i also know zero Go but the first example made sense (though while i don't know Go, i do know it has multiple return values): the "m[key]" part returns two values which are assigned to the "_, ok" expression which represents a value to be ignored and an "ok" variable which is later used in the "; ok" part (i guess ; separates multiple subexpressions similar to C's "," and the overall expression's value is the last subexpression) and so the two values returned from "m[key]" are the map's value and a boolean (or something that can be used like that) that checks if the value was found. In which case "if _,ok := m[key] {" would be the same as something like "if m.contains(key) {".

Of course this is all guesswork based on similar stuff i've seen elsewhere, so i might be off, but i think despite not knowing the language it still looks readable if you know any other mainstream language (and exercise some guesswork/imagination :-P).

Well, that's because you do not work with go much I guess?

I can say the same about Fennel, List or Haskell.

The first example is the way to go in Go. It's idiomatic, not a hack. And it works the same way for many things (for example checking if a channel's closed)

It is deeply idiomatic in the language.
But only because there's no `contains` function!
If perl has taught us anything... 3 ways to do something is worse than 1.
what `contains` what?

you could write this in Go

   if m[key] { }
the comma ok idiom simply lets you distinguish a "zero" value from a missing value.
Agreed that `key in m` or `m.contains(key)` would be clearer, but checking the value rather than the presence is asking for trouble.
`if m[key] { }` is only valid Go if m is a map from something to bool, so I don't know what you even mean.