-
Notifications
You must be signed in to change notification settings - Fork 2
/
body-inner.html
304 lines (234 loc) · 68.2 KB
/
body-inner.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<div style="max-width: 800px; margin: 0 auto 100px auto;"><style type="text/css">
header.header {
font-family: inherit;
font-size: 14px;
margin-top: 30px;
margin-bottom: 30px;
}
header.header a {
color: rgb(35, 35, 35);
text-decoration: none;
font-weight: bold;
}
header.header a:hover {
color: #4183c4;
}
header.header ul.nav {
display: inline-block;
margin-top: 0;
margin-bottom: 0;
padding-left: 0;
}
header.header li.nav {
display: inline-block;
margin-left: 20px;
}
header.header .nav.smaller {
font-size: smaller;
}
header.header .user {
float: right;
padding-top: 8px;
}</style><header class="header"><a href="/" style="display: inline-block;" class="Logo"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" width="32" height="32" style="fill: currentColor;
stroke: currentColor;
vertical-align: middle;"><circle cx="100" cy="100" r="90" stroke-width="20" fill="none"></circle><circle cx="100" cy="100" r="60"></circle></svg></a><ul class="nav"><li class="nav"><a href="/packages">Packages</a></li><li class="nav"><a href="/blog">Blog</a></li><li class="nav smaller"><a href="/idiomatic-go">Idiomatic Go</a></li><li class="nav"><a href="/talks">Talks</a></li><li class="nav"><a href="/projects">Projects</a></li><li class="nav"><a href="/resume">Resume</a></li><li class="nav"><a href="/about">About</a></li></ul><span class="user"><span style="margin-right: 10px;"><a href="/notifications" style="display: inline-block;
vertical-align: top;
position: relative;" title="You have no unread notifications."><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 14 16" style="fill: currentColor; vertical-align: top;"><path d="M13.99 11.991v1H0v-1l.73-.58c.769-.769.809-2.547 1.189-4.416.77-3.767 4.077-4.996 4.077-4.996 0-.55.45-1 .999-1 .55 0 1 .45 1 1 0 0 3.387 1.229 4.156 4.996.38 1.879.42 3.657 1.19 4.417l.659.58h-.01zM6.995 15.99c1.11 0 1.999-.89 1.999-1.999H4.996c0 1.11.89 1.999 1.999 1.999z"></path></svg></a></span><a href="" style="margin-right: 6px;"><img src="https://secure.gravatar.com/avatar?d=mm&f=y&s=96" title="Signed in as 1@example.org." style="border-radius: 2px;
width: 18px;
height: 18px;
vertical-align: top;"/></a><form method="post" action="/logout" style="display: inline-block; margin-bottom: 0;"><button type="submit" style="font-family: inherit;
font-size: 11px;
line-height: 11px;
height: 18px;
border-radius: 4px;
border: solid #d2d2d2 1px;
background-color: #fff;
box-shadow: 0 1px 1px rgba(0, 0, 0, .05);">Sign out</button><input type="hidden" name="return" value="http://localhost:8080/idiomatic-go"/></form></span></header><div class="markdown-body markdown-header-anchor" style="margin-bottom: 60px;"><h1><a name="idiomatic-go" class="anchor" href="#idiomatic-go" rel="nofollow" aria-hidden="true"><span class="octicon octicon-link"></span></a>Idiomatic Go</h1>
<p>When reviewing Go code, if I run into a situation where I see an unnecessary deviation from
idiomatic Go style or best practice, I add an entry here complete with some rationale, and
link to it.</p>
<p>I can do this for the smallest and most subtle of details, since I care about Go a lot. I can
reuse this each time the same issue comes up, instead of having to re-write the rationale
multiple times, or skip explaining why I make a given suggestion.</p>
<p>You can view this as my supplement to <a href="https://github.com/golang/go/wiki/CodeReviewComments" rel="nofollow">https://github.com/golang/go/wiki/CodeReviewComments</a>.</p>
<p>This page is generated from the entries with label "Accepted" <a href="/idiomatic-go/entries" rel="nofollow">here</a>.
If you'd like to add a new suggestion, please provide convincing rationale and references
(e.g., links to places in Go project that support your suggestion), and open a new issue.
It'll show up here when I add an "Accepted" label.</p>
<ul><li><a href="#use-consistent-spelling-of-certain-words">Use consistent spelling of certain words</a></li><li><a href="#single-spaces-between-sentences">Single spaces between sentences</a></li><li><a href="#error-variable-naming">Error variable naming</a></li><li><a href="#for-brands-or-words-with-more-than-1-capital-letter-lowercase-all-letters">For brands or words with more than 1 capital letter, lowercase all letters</a></li><li><a href="#comments-for-humans-always-have-a-single-space-after-the-slashes">Comments for humans always have a single space after the slashes</a></li><li><a href="#use-defer-if-its-overhead-is-relatively-negligible-or-it-buys-meaningful-readability">Use defer if its overhead is relatively negligible, or it buys meaningful readability</a></li><li><a href="#use-singular-form-for-collection-repo-folder-name">Use singular form for collection repo/folder name</a></li><li><a href="#avoid-unused-method-receiver-names">Avoid unused method receiver names</a></li><li><a href="#empty-string-check">Empty string check</a></li><li><a href="#mutex-hat">Mutex hat</a></li><li><a href="#don-t-os-exit-2-inside-flag-usage">Don't os.Exit(2) inside flag.Usage</a></li></ul></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="use-consistent-spelling-of-certain-words" class="anchor" href="#use-consistent-spelling-of-certain-words" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Use consistent spelling of certain words</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<pre><code>// marshaling
// unmarshaling
// canceling
// cancelation
</code></pre>
<p>Don't do this:</p>
<pre><code>// marshalling
// unmarshalling
// cancelling
// cancellation
</code></pre>
<p>For consistency with the Go project. These words have multiple valid spellings. The Go project picks one. See <a href="https://golang.org/cl/14150" rel="nofollow">CL 14150</a>, <a href="https://github.com/golang/go/issues/17938" rel="nofollow">issue 17938</a>. Also compare <a href="https://github.com/golang/go/search?q=marshaling+OR+unmarshaling" rel="nofollow">this</a> vs <a href="https://github.com/golang/go/search?q=marshalling+OR+unmarshalling" rel="nofollow">this</a>, and <a href="https://github.com/golang/go/search?q=cancelation" rel="nofollow">this</a> vs <a href="https://github.com/golang/go/search?q=cancellation" rel="nofollow">this</a>.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-12" class="reactable-container" data-reactableID="12"><a class="reaction" href="javascript:" title="1924134@github.com and 688231@github.com reacted with :triangular_ruler:." onclick="ToggleReaction(this, event, '"triangular_ruler"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 52.5% 57.5%;"></span></span><strong>2</strong></div></a><a class="reaction" href="javascript:" title="1924134@github.com and 627891@github.com reacted with :memo:." onclick="ToggleReaction(this, event, '"memo"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 52.5% 90%;"></span></span><strong>2</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"12"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/12" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>0 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="single-spaces-between-sentences" class="anchor" href="#single-spaces-between-sentences" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Single spaces between sentences</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<pre><code>// Sentence one. Sentence two.
</code></pre>
<p>Don't do this:</p>
<pre><code>// Sentence one. Sentence two.
</code></pre>
<p>For consistency with Go style. See <a href="https://golang.org/cl/20022" rel="nofollow">CL 20022</a>.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-10" class="reactable-container" data-reactableID="10"><a class="reaction" href="javascript:" title="288652@github.com, 1244900@github.com, 12886556@github.com, 607868@github.com, 245435@github.com, 2150567@github.com, 3162413@github.com, 52852@github.com, 6508@github.com, 848535@github.com and 2463838@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>11</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="10286538@github.com, 2359543@github.com, 943597@github.com, 1928691@github.com, 1244900@github.com, 12886556@github.com, 12271@github.com, 245435@github.com, 4652694@github.com and 1047261@github.com reacted with :heart:." onclick="ToggleReaction(this, event, '"heart"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 10% 22.5%;"></span></span><strong>10</strong></div></a><a class="reaction" href="javascript:" title="688231@github.com and 6508@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>2</strong></div></a><a class="reaction" href="javascript:" title="1329955@github.com reacted with :zzz:." onclick="ToggleReaction(this, event, '"zzz"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 50% 37.5%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"10"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/10" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>0 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="error-variable-naming" class="anchor" href="#error-variable-naming" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Error variable naming</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<div class="highlight highlight-Go"><pre><span class="c">// Package level exported error.</span>
<span class="k">var</span> <span class="n">ErrSomething</span> <span class="k">=</span> <span class="n">errors</span><span class="n">.</span><span class="n">New</span><span class="n">(</span><span class="s">"something went wrong"</span><span class="n">)</span>
<span class="k">func</span> <span class="n">main</span><span class="n">(</span><span class="n">)</span> <span class="n">{</span>
<span class="c">// Normally you call it just "err",</span>
<span class="n">result</span><span class="n">,</span> <span class="n">err</span> <span class="k">:=</span> <span class="n">doSomething</span><span class="n">(</span><span class="n">)</span>
<span class="c">// and use err right away.</span>
<span class="c">// But if you want to give it a longer name, use "somethingError".</span>
<span class="k">var</span> <span class="n">specificError</span> <span class="n">error</span>
<span class="n">result</span><span class="n">,</span> <span class="n">specificError</span> <span class="k">=</span> <span class="n">doSpecificThing</span><span class="n">(</span><span class="n">)</span>
<span class="c">// ... use specificError later.</span>
<span class="n">}</span>
</pre></div>
<p>Don't do this:</p>
<div class="highlight highlight-Go"><pre><span class="k">var</span> <span class="n">ErrorSomething</span> <span class="k">=</span> <span class="n">errors</span><span class="n">.</span><span class="n">New</span><span class="n">(</span><span class="s">"something went wrong"</span><span class="n">)</span>
<span class="k">var</span> <span class="n">SomethingErr</span> <span class="k">=</span> <span class="n">errors</span><span class="n">.</span><span class="n">New</span><span class="n">(</span><span class="s">"something went wrong"</span><span class="n">)</span>
<span class="k">var</span> <span class="n">SomethingError</span> <span class="k">=</span> <span class="n">errors</span><span class="n">.</span><span class="n">New</span><span class="n">(</span><span class="s">"something went wrong"</span><span class="n">)</span>
<span class="k">func</span> <span class="n">main</span><span class="n">(</span><span class="n">)</span> <span class="n">{</span>
<span class="k">var</span> <span class="n">specificErr</span> <span class="n">error</span>
<span class="n">result</span><span class="n">,</span> <span class="n">specificErr</span> <span class="k">=</span> <span class="n">doSpecificThing</span><span class="n">(</span><span class="n">)</span>
<span class="k">var</span> <span class="n">errSpecific</span> <span class="n">error</span>
<span class="n">result</span><span class="n">,</span> <span class="n">errSpecific</span> <span class="k">=</span> <span class="n">doSpecificThing</span><span class="n">(</span><span class="n">)</span>
<span class="k">var</span> <span class="n">errorSpecific</span> <span class="n">error</span>
<span class="n">result</span><span class="n">,</span> <span class="n">errorSpecific</span> <span class="k">=</span> <span class="n">doSpecificThing</span><span class="n">(</span><span class="n">)</span>
<span class="n">}</span>
</pre></div>
<p>For consistency. See <a href="https://talks.golang.org/2014/names.slide#15" rel="nofollow">https://talks.golang.org/2014/names.slide#15</a>.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-9" class="reactable-container" data-reactableID="9"><a class="reaction" href="javascript:" title="607868@github.com, 245435@github.com, 2150567@github.com, 3162413@github.com, 848535@github.com and 1329955@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>6</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="1767118@github.com, 1609368@github.com, 1928691@github.com, 12886556@github.com, 12271@github.com, 245435@github.com and 6508@github.com reacted with :clap:." onclick="ToggleReaction(this, event, '"clap"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 50%;"></span></span><strong>7</strong></div></a><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"9"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/9" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>0 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="for-brands-or-words-with-more-than-1-capital-letter-lowercase-all-letters" class="anchor" href="#for-brands-or-words-with-more-than-1-capital-letter-lowercase-all-letters" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>For brands or words with more than 1 capital letter, lowercase all letters</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<div class="highlight highlight-Go"><pre><span class="c">// Exported.</span>
<span class="k">var</span> <span class="n">OAuthEnabled</span> <span class="n">bool</span>
<span class="k">var</span> <span class="n">GitHubToken</span> <span class="n">string</span>
<span class="c">// Unexported.</span>
<span class="k">var</span> <span class="n">oauthEnabled</span> <span class="n">bool</span>
<span class="k">var</span> <span class="n">githubToken</span> <span class="n">string</span>
</pre></div>
<p>Don't do this:</p>
<div class="highlight highlight-Go"><pre><span class="c">// Unexported.</span>
<span class="k">var</span> <span class="n">oAuthEnabled</span> <span class="n">bool</span>
<span class="k">var</span> <span class="n">gitHubToken</span> <span class="n">string</span>
</pre></div>
<p>Lowercase all letters, not only the first letter, of the first word when you want to make an unexported version.</p>
<p>For consistency with what the Go project does, and because it looks nicer. <code>oAuth</code> is just weird.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-8" class="reactable-container" data-reactableID="8"><a class="reaction" href="javascript:" title="12271@github.com, 607868@github.com, 245435@github.com, 2150567@github.com, 3162413@github.com and 6508@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>6</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="1329955@github.com reacted with :thinking_face:." onclick="ToggleReaction(this, event, '"thinking_face"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 80% 10%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"8"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/8" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>2 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="comments-for-humans-always-have-a-single-space-after-the-slashes" class="anchor" href="#comments-for-humans-always-have-a-single-space-after-the-slashes" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Comments for humans always have a single space after the slashes</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<pre><code>// This is a comment
// for humans.
</code></pre>
<p>Don't do this:</p>
<pre><code>//This is a comment
//for humans.
</code></pre>
<p>For consistency, and because the no space style is reserved for comment pragmas, for example:</p>
<pre><code>//go:generate go run gen.go
</code></pre>
<p>Temporarily commented out code should also have no spaces, but it's not meant to be committed.</p>
<p>See <a href="https://golang.org/cmd/compile/#hdr-Compiler_Directives" rel="nofollow">https://golang.org/cmd/compile/#hdr-Compiler_Directives</a>.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-7" class="reactable-container" data-reactableID="7"><a class="reaction" href="javascript:" title="1609368@github.com, 1244900@github.com, 12271@github.com, 607868@github.com, 245435@github.com, 2150567@github.com, 3162413@github.com, 52852@github.com, 1329955@github.com and 627891@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>10</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"7"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/7" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>0 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="use-defer-if-its-overhead-is-relatively-negligible-or-it-buys-meaningful-readability" class="anchor" href="#use-defer-if-its-overhead-is-relatively-negligible-or-it-buys-meaningful-readability" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Use defer if its overhead is relatively negligible, or it buys meaningful readability</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Don't use it if the readability stays the same, as is the case in short 1-2 exit funcs.</p>
<p>Using it has a fixed performance overhead, which is noticeable for operations that run on the order of nanoseconds.</p>
<p>See <a href="https://lk4d4.darth.io/posts/defer/" rel="nofollow">https://lk4d4.darth.io/posts/defer/</a>.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-6" class="reactable-container" data-reactableID="6"><a class="reaction" href="javascript:" title="1609368@github.com, 1244900@github.com, 12271@github.com, 607868@github.com, 245435@github.com and 1329955@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>6</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="3162413@github.com reacted with :heart:." onclick="ToggleReaction(this, event, '"heart"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 10% 22.5%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"6"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/6" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>1 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="use-singular-form-for-collection-repo-folder-name" class="anchor" href="#use-singular-form-for-collection-repo-folder-name" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Use singular form for collection repo/folder name</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<pre><code>golang.org/x/example/hello
golang.org/x/example/outyet
golang.org/x/mobile/example/basic
golang.org/x/mobile/example/flappy
github.com/shurcooL/tictactoe/player/bad
github.com/shurcooL/tictactoe/player/random
</code></pre>
<p>Don't do this:</p>
<pre><code>golang.org/x/examples/hello
golang.org/x/examples/outyet
golang.org/x/mobile/examples/basic
golang.org/x/mobile/examples/flappy
github.com/shurcooL/tictactoe/players/bad
github.com/shurcooL/tictactoe/players/random
</code></pre>
<p>Notice how "example", "player" folder names simply contain many related packages, yet they use singular form.</p>
<p>Because it's consistent with the style that Go project uses.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-5" class="reactable-container" data-reactableID="5"><a class="reaction" href="javascript:" title="1244900@github.com, 12271@github.com, 607868@github.com, 245435@github.com, 2150567@github.com and 3162413@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>6</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="1767118@github.com, 12271@github.com, 245435@github.com and 1329955@github.com reacted with :clap:." onclick="ToggleReaction(this, event, '"clap"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 50%;"></span></span><strong>4</strong></div></a><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"5"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/5" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>2 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="avoid-unused-method-receiver-names" class="anchor" href="#avoid-unused-method-receiver-names" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Avoid unused method receiver names</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<div class="highlight highlight-Go"><pre><span class="k">func</span> <span class="n">(</span><span class="n">foo</span><span class="n">)</span> <span class="n">method</span><span class="n">(</span><span class="n">)</span> <span class="n">{</span>
<span class="k">...</span>
<span class="n">}</span>
</pre></div>
<p>Don't do this:</p>
<div class="highlight highlight-Go"><pre><span class="k">func</span> <span class="n">(</span><span class="n">f</span> <span class="n">foo</span><span class="n">)</span> <span class="n">method</span><span class="n">(</span><span class="n">)</span> <span class="n">{</span>
<span class="k">...</span>
<span class="n">}</span>
</pre></div>
<p>If <code>f</code> is unused. It's more readable because it's clear that fields or methods of <code>foo</code> are not used in <code>method</code>.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-4" class="reactable-container" data-reactableID="4"><a class="reaction" href="javascript:" title="607868@github.com, 245435@github.com, 2150567@github.com, 3162413@github.com, 52852@github.com and 848535@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>6</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="752919@github.com, 285414@github.com, 12271@github.com, 668723@github.com, 245435@github.com and 1329955@github.com reacted with :clap:." onclick="ToggleReaction(this, event, '"clap"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 50%;"></span></span><strong>6</strong></div></a><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="668723@github.com and 1329955@github.com reacted with :heart:." onclick="ToggleReaction(this, event, '"heart"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 10% 22.5%;"></span></span><strong>2</strong></div></a><a class="reaction" href="javascript:" title="1329955@github.com and 5557016@github.com reacted with :scream:." onclick="ToggleReaction(this, event, '"scream"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 67.5% 55%;"></span></span><strong>2</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"4"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/4" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>6 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="empty-string-check" class="anchor" href="#empty-string-check" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Empty string check</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<div class="highlight highlight-Go"><pre><span class="k">if</span> <span class="n">str</span> <span class="k">==</span> <span class="s">""</span> <span class="n">{</span>
<span class="k">...</span>
<span class="n">}</span>
</pre></div>
<p>Don't do this:</p>
<div class="highlight highlight-Go"><pre><span class="k">if</span> <span class="n">len</span><span class="n">(</span><span class="n">str</span><span class="n">)</span> <span class="k">==</span> <span class="m">0</span> <span class="n">{</span>
<span class="k">...</span>
<span class="n">}</span>
</pre></div>
<p>If you're checking if <code>str</code> is the empty string. Using <code>len(str)</code> is fine for other uses.</p>
<p>The first form is more readable because it's more obvious <code>str</code> is a string and not a slice.</p>
<p>See Russ Cox's comment in <a href="https://groups.google.com/forum/#!topic/golang-nuts/7Ks1iq2s7FA:" rel="nofollow">https://groups.google.com/forum/#!topic/golang-nuts/7Ks1iq2s7FA:</a></p>
<blockquote>
<p>if I care about "is it this specific string" I tend to write s == "".</p>
</blockquote>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-3" class="reactable-container" data-reactableID="3"><a class="reaction" href="javascript:" title="288652@github.com, 12271@github.com, 607868@github.com, 2150567@github.com, 848535@github.com, 2257149@github.com, 1329955@github.com and 5557016@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>8</strong></div></a><a class="reaction" href="javascript:" title="477998@github.com, 7011819@github.com, 1774000@github.com, 1244900@github.com, 1728787@github.com and 245435@github.com reacted with :-1:." onclick="ToggleReaction(this, event, '"-1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 35%;"></span></span><strong>6</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="10286538@github.com, 1767118@github.com, 245435@github.com and 2257149@github.com reacted with :v:." onclick="ToggleReaction(this, event, '"v"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 7.5% 50%;"></span></span><strong>4</strong></div></a><a class="reaction" href="javascript:" title="477998@github.com reacted with :hankey:." onclick="ToggleReaction(this, event, '"hankey"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 50% 50%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="1924134@github.com reacted with :point_left:." onclick="ToggleReaction(this, event, '"point_left"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 35% 47.5%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="1924134@github.com reacted with :smiley:." onclick="ToggleReaction(this, event, '"smiley"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 65% 42.5%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="58618@github.com reacted with :mask:." onclick="ToggleReaction(this, event, '"mask"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 67.5% 70%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="3162413@github.com and 1924134@github.com reacted with :laughing:." onclick="ToggleReaction(this, event, '"laughing"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 65% 50%;"></span></span><strong>2</strong></div></a><a class="reaction" href="javascript:" title="1329955@github.com reacted with :scream:." onclick="ToggleReaction(this, event, '"scream"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 67.5% 55%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"3"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/3" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>2 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="mutex-hat" class="anchor" href="#mutex-hat" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Mutex hat</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><div class="highlight highlight-Go"><pre><span class="k">struct</span> <span class="n">{</span>
<span class="k">...</span>
<span class="n">rateMu</span> <span class="n">sync</span><span class="n">.</span><span class="n">Mutex</span>
<span class="n">rateLimits</span> <span class="n">[</span><span class="n">categories</span><span class="n">]</span><span class="n">Rate</span>
<span class="n">mostRecent</span> <span class="n">rateLimitCategory</span>
<span class="n">}</span>
</pre></div>
<p>Here, <code>rateMu</code> is a mutex hat. It sits, like a hat, on top of the variables that it protects.</p>
<p>So, without needing to write the comment, the above is implicitly understood to be equivalent to:</p>
<div class="highlight highlight-Go"><pre><span class="k">struct</span> <span class="n">{</span>
<span class="k">...</span>
<span class="c">// rateMu protects rateLimits and mostRecent.</span>
<span class="n">rateMu</span> <span class="n">sync</span><span class="n">.</span><span class="n">Mutex</span>
<span class="n">rateLimits</span> <span class="n">[</span><span class="n">categories</span><span class="n">]</span><span class="n">Rate</span>
<span class="n">mostRecent</span> <span class="n">rateLimitCategory</span>
<span class="n">}</span>
</pre></div>
<p>When adding a new, unrelated field that isn't protected by <code>rateMu</code>, do this:</p>
<div class="highlight highlight-diff"><pre> struct {
...
<span class="gh input-block">
</span> rateMu sync.Mutex
rateLimits [categories]Rate
mostRecent rateLimitCategory
<span class="gi input-block"><span class="x"><span class="gd input-block"></span>+
+ common service
</span></span> }
</pre></div>
<p>Don't do this:</p>
<div class="highlight highlight-diff"><pre> struct {
...
<span class="gh input-block">
</span> rateMu sync.Mutex
rateLimits [categories]Rate
mostRecent rateLimitCategory
<span class="gi input-block"><span class="x"><span class="gd input-block"></span>+ common service
</span></span> }
</pre></div>
<p>See <a href="https://talks.golang.org/2014/readability.slide#21" rel="nofollow">https://talks.golang.org/2014/readability.slide#21</a>.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-2" class="reactable-container" data-reactableID="2"><a class="reaction" href="javascript:" title="607868@github.com, 245435@github.com, 3162413@github.com and 1329955@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>4</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="1924134@github.com reacted with :mute:." onclick="ToggleReaction(this, event, '"mute"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 55% 90%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="1924134@github.com reacted with :heavy_multiplication_x:." onclick="ToggleReaction(this, event, '"heavy_multiplication_x"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 7.5% 87.5%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="1924134@github.com, 58618@github.com, 23022@github.com and 12271@github.com reacted with :tophat:." onclick="ToggleReaction(this, event, '"tophat"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 22.5% 57.5%;"></span></span><strong>4</strong></div></a><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="2827521@github.com reacted with :raised_hands:." onclick="ToggleReaction(this, event, '"raised_hands"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 70% 70%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="1329955@github.com reacted with :grinning:." onclick="ToggleReaction(this, event, '"grinning"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 65% 35%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"2"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/2" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>4 comments</a></span></div><div class="markdown-body markdown-header-anchor" style="margin-bottom: 16px;"><h3><a name="don-t-os-exit-2-inside-flag-usage" class="anchor" href="#don-t-os-exit-2-inside-flag-usage" rel="nofollow" aria-hidden="true"><span class="octicon-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></span></a>Don't os.Exit(2) inside flag.Usage</h3></div><div class="markdown-body" style="padding-bottom: 10px; border-bottom: 1px solid #eee; margin-bottom: 8px;"><p>Do this:</p>
<div class="highlight highlight-Go"><pre><span class="n">flag</span><span class="n">.</span><span class="n">Usage</span> <span class="k">=</span> <span class="k">func</span><span class="n">(</span><span class="n">)</span> <span class="n">{</span>
<span class="n">fmt</span><span class="n">.</span><span class="n">Fprintf</span><span class="n">(</span><span class="n">os</span><span class="n">.</span><span class="n">Stderr</span><span class="n">,</span> <span class="s">"Usage of %s:\n"</span><span class="n">,</span> <span class="n">os</span><span class="n">.</span><span class="n">Args</span><span class="n">[</span><span class="m">0</span><span class="n">]</span><span class="n">)</span>
<span class="n">flag</span><span class="n">.</span><span class="n">PrintDefaults</span><span class="n">(</span><span class="n">)</span>
<span class="n">}</span>
</pre></div>
<p>Don't do this:</p>
<div class="highlight highlight-Go"><pre><span class="n">flag</span><span class="n">.</span><span class="n">Usage</span> <span class="k">=</span> <span class="k">func</span><span class="n">(</span><span class="n">)</span> <span class="n">{</span>
<span class="n">fmt</span><span class="n">.</span><span class="n">Fprintf</span><span class="n">(</span><span class="n">os</span><span class="n">.</span><span class="n">Stderr</span><span class="n">,</span> <span class="s">"Usage of %s:\n"</span><span class="n">,</span> <span class="n">os</span><span class="n">.</span><span class="n">Args</span><span class="n">[</span><span class="m">0</span><span class="n">]</span><span class="n">)</span>
<span class="n">flag</span><span class="n">.</span><span class="n">PrintDefaults</span><span class="n">(</span><span class="n">)</span>
<span class="n">os</span><span class="n">.</span><span class="n">Exit</span><span class="n">(</span><span class="m">2</span><span class="n">)</span>
<span class="n">}</span>
</pre></div>
<p>The <code>os.Exit</code> call is unnecessary because <code>flag</code> package is already responsible for doing it. See <a href="https://github.com/mdempsky/unconvert/commit/3b9aa41c71a855a5f5fee96dc751979ce6e8cbbe" rel="nofollow">here</a> and <a href="https://github.com/campoy/embedmd/pull/12/files" rel="nofollow">here</a> for examples.</p>
</div><div class="reaction-bar-appear" style="display: flex; justify-content: space-between; margin-bottom: 60px;"><div id="reactable-container-1" class="reactable-container" data-reactableID="1"><a class="reaction" href="javascript:" title="607868@github.com, 245435@github.com, 2150567@github.com and 1329955@github.com reacted with :+1:." onclick="ToggleReaction(this, event, '"+1"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 37.5% 20%;"></span></span><strong>4</strong></div></a><span style="margin-right: 4px;"></span><a class="reaction" href="javascript:" title="2902918@github.com reacted with :grinning:." onclick="ToggleReaction(this, event, '"grinning"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 65% 35%;"></span></span><strong>1</strong></div></a><a class="reaction" href="javascript:" title="688231@github.com reacted with :mushroom:." onclick="ToggleReaction(this, event, '"mushroom"');"><div class="reaction others"><span class="emoji-outer emoji-sizer"><span class="emoji-inner" style="background-position: 17.5% 10%;"></span></span><strong>1</strong></div></a><a class="new-reaction" href="javascript:" title="React" onclick="ShowReactionMenu(this, event, '"1"');"><div class="new-reaction"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;" class="smiley"><path d="M8 0C3.58 0 0 3.58 0 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm4.81 12.81a6.72 6.72 0 0 1-2.17 1.45c-.83.36-1.72.53-2.64.53-.92 0-1.81-.17-2.64-.53-.81-.34-1.55-.83-2.17-1.45a6.773 6.773 0 0 1-1.45-2.17A6.59 6.59 0 0 1 1.21 8c0-.92.17-1.81.53-2.64.34-.81.83-1.55 1.45-2.17.62-.62 1.36-1.11 2.17-1.45A6.59 6.59 0 0 1 8 1.21c.92 0 1.81.17 2.64.53.81.34 1.55.83 2.17 1.45.62.62 1.11 1.36 1.45 2.17.36.83.53 1.72.53 2.64 0 .92-.17 1.81-.53 2.64-.34.81-.83 1.55-1.45 2.17zM4 6.8v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2H5.2C4.53 8 4 7.47 4 6.8zm5 0v-.59c0-.66.53-1.19 1.2-1.19h.59c.66 0 1.19.53 1.19 1.19v.59c0 .67-.53 1.2-1.19 1.2h-.59C9.53 8 9 7.47 9 6.8zm4 3.2c-.72 1.88-2.91 3-5 3s-4.28-1.13-5-3c-.14-.39.23-1 .66-1h8.59c.41 0 .89.61.75 1z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 7 16" style="fill: currentColor; vertical-align: top;" class="plus"><path d="M4 4H3v3H0v1h3v3h1V8h3V7H4V4z"></path></svg></div></a></div><span class="black-link markdown-body" style="display: inline-block; margin-top: 4px; min-width: 150px; text-align: right;"><a href="/idiomatic-go/entries/1" style="line-height: 30px;"><span style="margin-right: 6px; position: relative; top: 7px;"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" style="fill: currentColor; vertical-align: top;"><path d="M15 1H6c-.55 0-1 .45-1 1v2H1c-.55 0-1 .45-1 1v6c0 .55.45 1 1 1h1v3l3-3h4c.55 0 1-.45 1-1V9h1l3 3V9h1c.55 0 1-.45 1-1V2c0-.55-.45-1-1-1zM9 11H4.5L3 12.5V11H1V5h4v3c0 .55.45 1 1 1h3v2zm6-3h-2v1.5L11.5 8H6V2h9v6z"></path></svg></span>2 comments</a></span></div></div>