3 ms·
Golang stdlib is an excellent reference for how to write idiomatic Go code https://github.com/golang/go/tree/master/src https://github.com/golang/go/tree/master
by shawnps 12y ago
Golang stdlib is an excellent reference for how to write idiomatic Go code https://github.com/golang/go/tree/master/src https://github.com/golang/go/tree/master/src
- fortytw2 12y agoIt's also easily among the most self-documenting code I've ever read
- donatj 12y agoIt's so beautifully understandable.
- frakkingcylons 12y agoMy experience with learning Go so far has involved: * Do the tour (tour.golang.org) * Read 'How to Write Go Code' * Try to write a simple, concurrent program (I went with a web crawler) * Read 'Effective Go', stdlib code And now I'm trying to make the concurrent web crawler distributed! It's a lot of fun so far.
- arvinsim 12y agoAs someone who never learned to code in C, should I be ashamed that I find it difficult to read code with pointers and references?
- deleted 12y ago[deleted]
- djb_hackernews 12y agoI write a lot of code and have an interest in new languages. I've looked in to Go quite a bit (especially since I spend a lot of time working with Docker) and I guess I might be missing something. Taking a random method here: https://github.com/golang/go/blob/master/src/strings/reader.go#L31 https://github.com/golang/go/blob/master/src/strings/reader.... Some questions that come to me when looking at that code: - In general, why is everything abbreviated? Storage isn't an issue and IDEs solve the typing problem. It feels really old school. Plus you lose a lot of meaning throughout the code (r.prevRune is actually an index, so why not reader.previousRuneIndex, or at least r.prevRuneIdx) - What is int64? A cast? Why would you need to cast what appears to already be an int? - what is a Rune? Why are we setting it to -1? -1 just seems like a special number that would make this more readable as a constant with a relevant name. - Where does copy come from? why does it appear to work backwards compared to any other language array copy implementation? - Why does the last return statement return nothing? I'm not here to start a language war, I truly believe it is my inability to see the forest for the trees, but I find Go code to be hard to read and understand.
- dradtke 12y ago1. Go encourages short variable names. I personally think that the names used in the example you pulled are a touch shorter than I myself would use, and "prevRune" is a little bit ambiguous, but I don't really mind it since the context makes it more clear. 2. Yes, int64() is a cast to an int64 type. The bare "int" type in Go is architecture-dependent, so the compiler will complain if you try to assign an int to an int64 without the cast. 3. Rune is a number representing a UTF-8 codepoint. It's an alias for int32 the way "byte" would be an alias for in8. (http://golang.org/doc/go1#rune http://golang.org/doc/go1#rune) 4. copy() is a built-in function. There's only a small handful of built-in functions defined, and they're all documented here: http://golang.org/pkg/builtin/ http://golang.org/pkg/builtin/ 5. The last return statement doesn't specify any values because the method signature has already assigned names to those: n for the int, and err for the error, so an empty return means "return the values of the variables n and err", which default to their respective type's 0 values (0 and nil for int and error). Returning the values explicitly still works as seen in that example, and is slightly shorter than "n = 0; err = io.EOF; return". Go can be tricky to read if you're not familiar with some of the semantics, but once you've ramped up a little bit, then things just start falling into place.