Most active commenters
  • OvervCW(5)
  • 0x696C6961(3)
  • liampulles(3)

←back to thread

Go subtleties

(harrisoncramer.me)
234 points darccio | 24 comments | | HN request time: 1.723s | source | bottom
1. DarkNova6 ◴[] No.45667140[source]
As somebody who only views Go from a distance, I see this list as a combination of „what‘s the big deal?“ and „please don‘t“.
replies(2): >>45667192 #>>45672374 #
2. OvervCW ◴[] No.45667192[source]
I'm amused by posts like this because it shows that Go is finally slowly moving away from being an unergonomically simplistic language (its original USP?) to adopt features a modern language should have had all along.

My experience developing in it always gave me the impression that the designers of the language looked at C and thought "all this is missing is garbage collection and then we'll have the perfect language".

I feel like a large amount of the feeling of productivity developers get from writing Go code originates from their sheer LOC output due to having to reproduce what other languages can do in just a few lines thanks to proper language & standard library features.

replies(8): >>45667300 #>>45667358 #>>45667372 #>>45667409 #>>45667468 #>>45667838 #>>45668913 #>>45675568 #
3. eptcyka ◴[] No.45667300[source]
How is the boxed interface problem enabling better ergonomics for Go? I find that most quirks listed here are not making the language any better.
replies(1): >>45667352 #
4. OvervCW ◴[] No.45667352{3}[source]
In this specific blog post I suppose only "Ranging Directly over Integers" counts, but I was more generally referring to the introduction of features like generics.
5. 0x696C6961 ◴[] No.45667358[source]
If you think Go and C are that similar then you don't know either.
replies(2): >>45667436 #>>45667558 #
6. pjmlp ◴[] No.45667372[source]
Unfortunely given that the authors are also related to C's creation, it shows a common pattern, including why C is an insecure language.

> Although we entertained occasional thoughts about implementing one of the major languages of the time like Fortran, PL/I, or Algol 68, such a project seemed hopelessly large for our resources: much simpler and smaller tools were called for. All these languages influenced our work, but it was more fun to do things on our own.

From https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist...

Go grew up from the failed design with Alef in Plan 9, which got a second chance with Limbo on Inferno.

https://en.wikipedia.org/wiki/Alef_(programming_language)

> Rob Pike later explained Alef's demise by pointing to its lack of automatic memory management, despite Pike's and other people's urging Winterbottom to add garbage collection to the language;

https://doc.cat-v.org/inferno/4th_edition/limbo_language/lim...

You will notice some of the similarities between Limbo and Go, with a little sprikle of Oberon-2 method syntax, and SYSTEM replaced by unsafe.

https://ssw.jku.at/Research/Papers/Oberon2.pdf

7. ◴[] No.45667409[source]
8. quietbritishjim ◴[] No.45667436{3}[source]
Go and C have partially shared origins. Two of the three creators of Go (Ken Thompson and Rob Pike) were involved in the early days of C. Ken Thompson is even the creator of B, the predecessor of C. There are obvious huge differences between the language but in a more subtle way they're actually quite similar: C is an "unergonomically simplistic language", just as the parent commenter describes Go.
replies(1): >>45668760 #
9. petralithic ◴[] No.45667468[source]
More like, let's throw away the last 75 years of programming language theory advances, only to rediscover them again ourselves, with much hardship.
replies(1): >>45668392 #
10. OvervCW ◴[] No.45667558{3}[source]
They are similar in the sense that there are very few abstractions, relying on the programmer to reimplement common patterns and avoid logical mistakes.

You have to put thought into such things as:

- Did I add explicit checks for all the errors my function calls might return?

- Are all of my resources (e.g. file handles) cleaned up properly in all scenarios? Or did I forget a "defer file.Close()"? (A language like C++ solved this problem with RAII in the 1980s)

- Does my Go channel spaghetti properly implement a worker pool system with the right semaphores and error handling?

replies(1): >>45668701 #
11. DanielHB ◴[] No.45667838[source]
I remember when I first got out of uni and did backend Java development, I thought I was incredibly productive because of the sheer amount of typing and code I had to pump out.

After doing a bit of frontend JS I was quickly dissuaded of that notion, all I was doing was writing really long boilerplate.

This was in the Java 6 days, so before a lot of nice features were added, for example a simple callback required the creation of a class that implements an interface with the method (so 3 unique names and a bunch of boilerplate to type out, you could get away with 2 names if you used an anonymous class).

12. DarkNova6 ◴[] No.45668392{3}[source]
Sounds Like „Tell me about Generics in Go without telling me about Generics in Go“
replies(1): >>45668723 #
13. 0x696C6961 ◴[] No.45668701{4}[source]
> Did I add explicit checks for all the errors my function calls might return?

You can easily check this with a linter.

> Are all of my resources (e.g. file handles) cleaned up properly in all scenarios? Or did I forget a "defer file.Close()"? (A language like C++ solved this problem with RAII in the 1980s)

You can forget to use `with` in Python, I guess that's also C now too eh?

> Does my Go channel spaghetti properly implement a worker pool system with the right semaphores and error handling?

Then stop writing spaghetti and use a higher level abstraction like `x/sync/errgroup.Group`.

replies(1): >>45668971 #
14. OvervCW ◴[] No.45668723{4}[source]
No need to talk about generics when we can talk about something simple like the inability to implement type safe enums in Go.
15. 0x696C6961 ◴[] No.45668760{4}[source]
Pike was not involved with the design of C. He was involved with Newsqueak and Limbo which inspired Go's concurrency model.
16. liampulles ◴[] No.45668913[source]
As a Go developer, I do think that I end up writing more code initially, not just because of the lack of syntactic sugar and "language magic", but because the community philosophy is to prefer a little bit of copying over premature abstraction.

I think the end result is code which is quite easy to understand and maintain, because it is quite plain stuff with a clear control flow at the end of the day. Go code is the most pleasant code to debug of all the languages I've worked with, and there is not a close second.

Given that I spend much more time in the maintenance phase, it's a trade-off I'm quite happy to make.

(This is of course all my experience; very IMO)

replies(1): >>45669075 #
17. OvervCW ◴[] No.45668971{5}[source]
>Did I add explicit checks for all the errors my function calls might return?

You can check anything with a linter, but it's better when the language disallows you from making the mistake in the first place.

>You can forget to use `with` in Python, I guess that's also C now too eh?

When using `with` in Python you don't have to think about what exactly needs to be cleaned up, and it'll happen automatically when there is any kind of error. Consider `http.Get` in Go:

resp, err := http.Get(url)

if err == nil { resp.Body.Close() }

return err

Here you need to specifically remember to call `resp.Body.Close` and in which case to call it. Needlessly complicated.

>Then stop writing spaghetti and use a higher level abstraction like `x/sync/errgroup.Group`.

Why is this not part of the standard library? And why does it not implement basic functionality like collecting results?

replies(1): >>45669722 #
18. miohtama ◴[] No.45669075{3}[source]
How much is premature in time? 10 years? 20, 30 years?
replies(1): >>45669274 #
19. liampulles ◴[] No.45669274{4}[source]
So for me, the question is: are these two things intrinsically the same or coincidentally the same? If it is intrinsically the same, then an abstraction/centralization of logic is correct. If they are coincidentally the same, then its better to keep them separate.

Its premature if I don't know the answer to that question with my current information, which is a common scenario for me when I'm initially writing a new set of usecases.

If I get a 3rd copy of a thing, then its likely going to become an abstraction (and I'll probably have better understanding of the thing at the time to do that abstraction). If I don't get a 3rd copy of that thing, then its probably fine for the thing to be copied in 2 places, regardless of what the answer to my question is.

replies(1): >>45675466 #
20. amiga386 ◴[] No.45669722{6}[source]
This seems like a judiciously designed API to me.

You don't need to check if err was nil before calling resp.Body.Close()

https://pkg.go.dev/net/http#Get

> When err is nil, resp always contains a non-nil resp.Body. Caller should close resp.Body when done reading from it.

https://pkg.go.dev/net/http#Response

> The http Client and Transport guarantee that Body is always non-nil, even on responses without a body or responses with a zero-length body. It is the caller's responsibility to close Body.

Calling http.Get() returns an object that symbolises the response. The response body itself might be multiple terabytes, so http.Get() shouldn't read it for you, but give you a Reader of some sort.

The question then is, when does the Reader get closed? The answer should be "when the caller is done with it". This can't be automatic handled when the resp object goes out of scope, as it would preclude the caller e.g. passing the response to another goroutine for handling, or putting it in an array, or similar.

Go tooling is more than happy to tell you that there's an io.ReadCloser in one of the structs returned to you, and it can see that you didn't Close() it, store it, or pass it to somewhere else, before the struct it was in went out of scope.

21. tptacek ◴[] No.45672374[source]
If you don't write Go at all, this blog post isn't going to be useful to you, and you aren't its audience. It's fine not to have an apt take for a programming-language-specific article!
22. ted_dunning ◴[] No.45675466{5}[source]
On the other hand, when your abstraction has more configuration options than methods, it is a sign that (years ago) it really wasn't an abstract at all.
replies(1): >>45678868 #
23. deivid ◴[] No.45675568[source]
C at least has const ptr. In go I've seen pointers mutated 7 levels down the callstack. And of course, the rest of the sphagetti depended on those side effects.

C is so limited that you would try to avoid mutation and even complex datastructures.

Go is "powerful" enough to let you shoot yourself much harder.

Go with `const` and NonNull<ptr> (call it a reference if you need) would be a much nicer language

24. liampulles ◴[] No.45678868{6}[source]
Totally agree.