←back to thread

Go is still not good

(blog.habets.se)
644 points ustad | 1 comments | | HN request time: 0.203s | source
Show context
torginus ◴[] No.44983258[source]
I still don't understand why defer works on function scope, and not lexical scope, and nobody has been able to explain to me the reason for it.

In fact this was so surprising to me is that I only found out about it when I wrote code that processed files in a loop, and it started crashing once the list of files got too big, because defer didnt close the handles until the function returned.

When I asked some other Go programmers, they told me to wrap the loop body in an anonymus func and invoke that.

Other than that (and some other niggles), I find Go a pleasant, compact language, with an efficient syntax, that kind of doesn't really encourage people trying to be cute. I started my Go journey rewriting a fairly substantial C# project, and was surprised to learn that despite it having like 10% of the features of C#, the code ended up being smaller. It also encourages performant defaults, like not forcing GC allocation at every turn, very good and built-in support for codegen for stuff like serialization, and no insistence to 'eat the world' like C# does with stuff like ORMs that showcase you can write C# instead of SQL for RDBMS and doing GRPC by annotating C# objects. In Go, you do SQL by writing SQL, and you od GRPC by writing protobuf specs.

replies(7): >>44983266 #>>44983314 #>>44983343 #>>44983484 #>>44984652 #>>44985794 #>>44992509 #
__s ◴[] No.44984652[source]
1. it avoids a level of indentation until you wrap it in a function

2. mechanic is tied to call stack / stack unwinding

3. it feels natural when you're coming from C with `goto fail`

(yes it annoys me when I want to defer in a loop & now that loop body needs to be a function)

replies(1): >>44988968 #
1. torginus ◴[] No.44988968[source]
I think you hit the nail on the head - I think it's the stupid decision on Go lang designers part to make panic-s recover-able. This necessitates stack unwinding, meaning defer-s still need to run if a panic happens down the stack.

Since they didn't want to have a 'proper' RAII unwinding mechanism, this is the crappy compromise they came up with.