Hacker News new | ask | show | jobs
by nemo1618 1982 days ago
The best Go source code is the stdlib. When you read stdlib code, you can be confident that nothing is there by accident. Every decision was made for a reason. When you're learning, that's invaluable, because it means you can "dig" anywhere and be rewarded for it, whereas "digging" into most codebases will often be a waste of time.

Also, I would advise actively avoiding the big names you have heard of. A lot of products are successful despite having garbage code. And even more of them are successful despite having only decent code, full of stuff not worth emulating.

Try to identify people who have a lot of experience and a strong command of the language, then look at their most recent projects.

5 comments

The stdlib is kind of a mess. One kitchen, many cooks, and everyone was learning how to put a five-course meal together as they went. There are some great bits -- package io and ioutil, for example -- but a lot of it is full of what we now understand are bad ideas, and the compatibility promise prevents anyone from doing anything about it.

There is much better code to read. I second the recommendation of HashiCorp's stuff. Stay away from Docker and especially Kubernetes.

FYI, ioutil is deprecated in Go 1.16. :-) They copied everything out of it and into io and os. It’s much cleaner actually once you get used to it.
Another reason it is a bit of a mess is because of the no breaking changes promise. They have to keep ugly or deprecated interfaces unchanged, even into Go 2, iirc.
Couldn't agree more. Cheney's response to why that is was "do as we say, not as we do".
Do you have source / more details? There certainly are a few warts but overall it gets the job done.
The stdlib is the best for clarity. But there are some excellent examples of how to structure projects depending on what you're trying to build. Mat Ryer gave an excellent talk on writing HTTP Services. If you want a good example on how to write distributed systems then Hashicorps' projects are more or less gorgeous.

https://medium.com/@matryer/how-i-write-go-http-services-aft...

https://github.com/hashicorp

> Hashicorps' projects are more or less gorgeous

Is there a particular piece of code (as in, a single package/file/function) that jumps out to you as "gorgeous?" When I look at a random Hashicorp repo, it strikes me an impenetrable labyrinth.

Try to add a new feature or see how some feature works, with consul I did some greps over some features I used and followed the code.. was able to put a merge request upstream changing a behaviour I didn't like (configurable) within 4h (that didn't get accepted and that's OK)
To add to your point, Nic Jackson from hashicorp has a bunch of how-to videos and presentations that I've found good also.

https://www.youtube.com/channel/UC2V1SxXFUa5YxVJvTsrCgyg

He also wrote a book: https://www.packtpub.com/product/building-microservices-with...

The book might be good, but my copy had a problem with spurious characters in every code block, but that seems to be fixed in later printings.

Great resource! Thanks.
> The best Go source code is the stdlib.

As a golang beginner, the go stdlib is one the most readable stdlibs I've read through. The combination of gofmt, manageable language features, and idiomatic ways to do things make most go code fairly easy to read.

I know go doesn't have a million language features, but readability after the fact is one of the benefits of this approach.

It’s also one of the more readable stdlibs. I use it for idioms and snippets .
I second that. Stdlib is great to get a feel how the language works. Some small programs are always the best, if they are written by a good programmer. A cli game i study right now but in Rust. A small program with a lot of logic, but without all the graphics distractions.

https://github.com/VladimirMarkelov/solkit