Live data from Hacker News

Go Style

google.github.io

121–130 of 212 posts

Re: Go Style

#121
My favorite Easter egg when helping to write the style guide was including a reference to Full Metal Jacket within it. See if you can find it.

In all seriousness, I am very proud of the team’s work in both writing and publishing this externally. It was a lot of work.

Re: Go Style

#122
post #116
post #107

Earlier quoted context omitted.

That wouldn't pass a code review where I work... Use a defer to do the unlock

Does defer get called on panic? I thought panic cancels all further execution

Yes it does, which is why recovering from a panic can be done in a deferred function. The go runtime maintains enough metadata to track what deferred functions need to be run while unwinding the stack.

Re: Go Style

#124

In my opinion the hardest style rules to accept when trying to use this guide are: 1. Do not create "assertion libraries" like `assertEqual(x, y)` [1] 2. Leave testing to the Test function [2] 3. Intialisms (HTTPURL, IOS, gRPC) [3] 4. Function formatting [4] For the record I'm not saying I disagree with these. I just think that folks coming from other languages have a lot of built in muscle memory to do it other ways…

If you want context on why some testing guidance is the way it is, this external discussion may be helpful: https://www.reddit.com/r/golang/comments/yy8edb/googles_inte....

Re: Go Style

#125
post #45

> A little copying is better than a little dependency. Go takes this to an extreme, though. Generics helps, but the implementation is so limiting that it doesn't help much. > The general rule of thumb is that the length of a name should be proportional to the size of its scope and inversely proportional to the number of times that it is used within that scope. Some (most) of the go code I've seen where I work suffers…

This is probably because Go doesn't allow function overriding. You can't have `streamFromUrl(foo)` and `streamFromUrl(foo, bar)`, so you end up with `streamFromUrlWithFoo(foo)` and streamFromUrlWithFooAndBar(foo, bar)`.

No doubt: and that is generally the pattern I’ve seen. It’s really an eyesore.

Re: Go Style

#126
has anyone compiled a comprehensive list of style guides from leading companies?

google has published a style guide, but it doesn't seem like facebook and netflix do.

it would be awesome to have one central list of best practices from leading tech companies.

we started one on github, but it's woefully limited: https://github.com/HotpotDesign/Developer-Style-Guides

could anyone kindly recommend better ones?

Re: Go Style

#127

Earlier quoted context omitted.

This is probably because Go doesn't allow function overriding. You can't have `streamFromUrl(foo)` and `streamFromUrl(foo, bar)`, so you end up with `streamFromUrlWithFoo(foo)` and streamFromUrlWithFooAndBar(foo, bar)`.

Consider variadic arguments for such cases.

There is a group of developers where I work who would take a perfectly fine `New(…*Options)` constructor and write a whole suite of “option wrapper” “utilities” around it, rather than expose that function. It makes me tear up a little (not in a good way).

Re: Go Style

#128

> The general rule of thumb is that the length of a name should be proportional to the size of its scope and inversely proportional to the number of times that it is used within that scope. > A variable created at file scope may require multiple words, whereas a variable scoped to a single inner block may be a single word or even just a character or two, to keep the code clear and avoid extraneous information. I love…

This advice should be universal in coding. When I first started programming, I had a manager that hated 1-2 character variables. But they make sense for loop iterators.

Yeah i for for loops (in python) x for comprehensions, etc, make sense. But in general, single character variables make code really hard to read.

Re: Go Style

#129

Earlier quoted context omitted.

I think you’re reading this too literally. They likely just mean file scope from a purely usage perspective. I.E. you only use the variable in one file, it doesn’t matter how the language processes it into a computational scope

It absolutely does matter. If I'm editing the file in which the package-scope variable (or type, or const) is declared (and also referenced) and decide on the fly to subtly change its semantics, forgetting that the same is also referenced from some other file within the package, I have likely just created a bug. Worse, the bug will likely manifest itself only at run time.

You're still taking things too literally.

A human typed in something less precise than you'd like in a style guide. Find a bug tracker to request a verbiage change. It's not that big of a deal.

No one is asserting that variables are in fact file scoped.

Re: Go Style

#130
Best practices should differentiate between libraries and applications. Otherwise, like this guide you assume every piece of code is a library and slow down application development.

For example they recommend not using %w for errors- because library authors need to be careful about what information is exposed. However, as an application code developer, %w should be used by default- this avoids an accidental bug where annotating an error could cause an upstream error check to fail- which they allude to in the guide- but now their rule is getting complicated whereas "default to %w" would be safer and improve velocity in application code.

Post reply on HN