-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2024-12-gophercon-india.slide
538 lines (335 loc) · 13.6 KB
/
2024-12-gophercon-india.slide
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# Operationalising golangci-lint
: @formatter:off
Leveraging Go Linters for Robust Code Quality
02 Dec 2024
Tags: linters, golangci-lint
Summary: Configuring golangci-lint for better Go code.
Suhas Karanth
Staff Engineer, Netskope
skaranth@netskope.com
https://github.com/sudo-suhas
https://www.linkedin.com/in/skaranth90/
: @formatter:on
## Agenda
- What are linters?
- Why use linters?
- Examples
- How to use `golangci-lint`?
## What are linters?
- Static code analysis tools
- Flag potential bugs and code style issues
Trivia: The term originates from a Unix utility that examined C language source code.
: Static code analysis means that we analyze the code without executing it.
## Why use linters?
- **[Shift-Left Testing][shift-left-testing]**
- **Code style**:
- Consistency
- Readability
- Slightly faster speed of delivery
- Avoid inconsequential debates (ex: `gofmt` for tabs vs spaces)
- **Guardrails**
[shift-left-testing]: https://www.drdobbs.com/shift-left-testing/184404768
: [Shift-Left Testing]:
: - Feature lifecycle - Product requirement gathering, architectural design, implementation, unit
: tests, integration tests etc.
: - Objective: Detect issues earlier
: - Rationale: Bugs are cheap when caught young
: - Win: Faster feedback => improved quality and speed of delivery.
: - Contextual: Move detection of bugs from unit tests/integration tests to development stage.
: [Consistency]:
: - Scope: code constructs + package/function/type names. Ex: Effective Go guidelines on package
: names.
: - Rationale: Consistency => readability => maintainability.
: [Guardrails]:
: - For both experienced and new developers, avoid tricky bugs
: - More helpful for newbies
: - Not all bugs are easily caught in tests either.
## Examples
: To understand why we would want to use linters, lets look at some examples. I am going to show
: some code snippets and I want you to try and spot the issue. The examples are a bit contrived but
: that should be okay for the purposes of this discussion. I will also show an example of a bug
: that is harder to catch in tests.
## Example #1
.code demo-code/snippets/errorsas.go /START BROKEN/,/END BROKEN/
## Example #1
.image assets/errorsas/errorsas-01.png _ 957
## Example #1
.image assets/errorsas/errorsas-02.png _ 957
## Example #1
.image assets/errorsas/errorsas-03.png _ 957
## Example #1
.image assets/errorsas/errorsas-04.png _ 957
## Example #1
.code demo-code/snippets/errorsas.go /START BROKEN/,/END BROKEN/
## Example #1
Issue can be fixed by passing a pointer to `*MyError`, the type implementing error.
.code demo-code/snippets/errorsas.go /START FIXED/,/END FIXED/
`go vet` can flag the issue in this case.
```
$ go tool vet help errorsas
errorsas: report passing non-pointer or non-error values to errors.As
The errorsas analysis reports calls to errors.As where the type
of the second argument is not a pointer to a type implementing error.
```
For the example from the previous slide, we get the following error:
```
snippets/errorsas.go:21:6: second argument to errors.As must be a non-nil pointer to either a
type that implements error, or to any interface type
```
: By default, `go test` runs a high-confidence subset of `go vet` checks which includes `errorsas`.
## Example #2
## Example #2
.code demo-code/urlqueryv1/main.go /START/,/END/
## Example #2
<p style="margin: -20px">
.image assets/urlquery/urlquery-01.png _ 979
</p>
## Example #2
<p style="margin: -20px">
.image assets/urlquery/urlquery-02.png _ 979
</p>
## Example #2
<p style="margin: -20px">
.image assets/urlquery/urlquery-03.png _ 979
</p>
## Example #2
<p style="margin: -20px">
.image assets/urlquery/urlquery-04.png _ 979
</p>
## Example #2
.play -edit demo-code/urlqueryv1/main.go /START/,/END/
## Example #2
One of the _many_ detections in [staticcheck][staticcheck-sa4027] can flag this:
[staticcheck-sa4027]: https://staticcheck.dev/docs/checks/#SA4027
```
snippets/tbd.go:15:2: SA4027: (*net/url.URL).Query returns a copy, modifying it doesn't change
the URL (staticcheck)
u.Query().Set("search", params.Search)
^
```
Small changes are needed to fix the issue:
.play -edit demo-code/urlqueryv2/main.go /START/,/END/
## Example #3
: Example where it is harder to catch the issue in tests
## Example #3
.code demo-code/rowserrv1/main.go /START FETCH/,/END FETCH/
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-01.png _ 979
</p>
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-02.png _ 979
</p>
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-03.png _ 979
</p>
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-04.png _ 979
</p>
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-05.png _ 979
</p>
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-06.png _ 979
</p>
## Example #3
.code demo-code/rowserrv1/main.go /START DEMO/,/END DEMO/
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-07.png _ 979
</p>
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-08.png _ 979
</p>
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-09.png _ 979
</p>
## Example #3
.play -edit demo-code/rowserrv1/main.go /START DEMO/,/END DEMO/
## Example #3
.code demo-code/rowserrv1/main.go /START FETCH/,/END FETCH/
## Example #3
Linter that can catch this issue - [rowserrcheck][rowserrcheck]:
```
rowserrv1/main.go:82:30: rows.Err must be checked (rowserrcheck)
rows, err := db.QueryContext(ctx, qry)
^
```
[rowserrcheck]: https://github.com/jingyugao/rowserrcheck
Issue can be fixed with a `rows.Err()` check:
.play -edit demo-code/rowserrv2/main.go /START/,/END/
## Example #3
<p style="margin: -20px">
.image assets/rowserr/rowserr-10.png _ 979
</p>
## Linting in Go
## Linting in Go
1. Why `golangci-lint`?
2. Why do we need extensive configuration?
3. Configuration builder sheet walkthrough.
## golangci-lint
[https://golangci-lint.run/][golangci-lint-home]
[golangci-lint-home]: https://golangci-lint.run/
<p style="margin-top: -15px;">
> _It runs linters in parallel, uses caching, supports YAML configuration,
> integrates with all major IDEs, and includes over a hundred linters._
</p>
<table style="margin-top: -10px;">
<tr>
<td><b>Popular</b></td>
<td>
.image assets/golangci-lint-popularity.png _ 520
</td>
</tr>
<tr>
<td><b>Actively maintained</b></td>
<td>
.image assets/golangci-lint-pulse-insights.png 380 _
</td>
</tr>
</table>
: What is golangci-lint? It is a linter aggregator that provides a performant, consistent interface
: for running 100+ linters.
## Adopting golangci-lint
Just run `golangci-lint` in your project and you are done.
```
$ golangci-lint run
```
_Right?_
: Hopefully, I have convinced you about using golangci-lint. So just run golangci-lint in your
: project and you are done, right?
## Need for configuration
Unfortunately, just running `golangci-lint` is not enough.
**Default configuration**
`golangci-lint` enables a very small set of linters by default:
- [errcheck][errcheck]
- [gosimple][gosimple]
- [govet][govet]
- [ineffassign][ineffassign]
- [staticcheck][staticcheck]
- [unused][unused]
[errcheck]: https://github.com/kisielk/errcheck
[gosimple]: https://github.com/dominikh/go-tools/tree/master/simple
[govet]: https://pkg.go.dev/cmd/vet
[ineffassign]: https://github.com/gordonklaus/ineffassign
[staticcheck]: https://staticcheck.io/
[unused]: https://github.com/dominikh/go-tools/tree/master/unused
## Useful linters disabled by default
There are many more extremely useful linters that are disabled by default. Some examples:
- [errorlint][errorlint]: for error wrapping scheme introduced in Go 1.13.
- [gocognit][gocognit]: enforce limit on cognitive complexity of functions.
- [gofumpt][gofumpt]: a stricter gofmt.
- [gosec][gosec]: inspect source for **security issues**.
- [noctx][noctx]: find HTTP requests without `context.Context`.
- [rowserrcheck][rowserrcheck], [sqlclosecheck][sqlclosecheck]: ensure correct usage of
`database/sql` package.
- [gocritic][gocritic]: 100+ diagnostic checks
- [revive][revive]: faster, configurable golint
[errorlint]: https://github.com/polyfloyd/go-errorlint
[gocognit]: https://github.com/uudashr/gocognit
[gofumpt]: https://github.com/mvdan/gofumpt
[gosec]: https://github.com/securego/gosec
[noctx]: https://github.com/sonatard/noctx
[rowserrcheck]: https://github.com/jingyugao/rowserrcheck
[sqlclosecheck]: https://github.com/ryanrolds/sqlclosecheck
[gocritic]: https://github.com/go-critic/go-critic
[revive]: https://github.com/mgechev/revive
: These linters can help uncover bugs and ensure consistent code style.
: gosec deserves a special mention since it is able to catch security issues like SQL and command
: injection.
## So just enable everything?
## So just enable everything?
No, for a few reasons:
- **Noise:** Not all linters match _your_ style.
- **Ineffective:** Some linters are useful only in a specific context.
- **Duplication:** Some checks are duplicated between different linters.
- **Premature optimisation:** Use in constrained execution environments.
[decorder]: https://gitlab.com/bosi/decorder
[zerologlint]: https://github.com/ykadowak/zerologlint
[prealloc]: https://github.com/alexkohler/prealloc
: - [Noise]: Opinions might differ. False positives. exhaustruct
: - [Ineffective]: zerologlint for zerolog
: - [Duplication]: govet and revive both have a structtag check.
: - [Premature optimisation]: prealloc
: I know of 40+ checks that are duplicated across different linters. Disabling duplicate checks and
: linters has a performance benefit as well.
: I hope you can see why we need to configure golangci-lint to enable the linters that we want. But
: there is 1 additional aspect I want to touch on w.r.t configuration.
## Individual linter configuration
For many of the linters, individual configuration is needed to get the desired behaviour.
For example, for `gofumpt`, we might want to have the following configuration:
```
# Choose whether to use the extra rules.
# Default: false
extra-rules = true
```
: This configuration is also necessary for linters that include multiple rules such as gocritic
: and revive because we would want a subset of the available rules.
## Configuration builder
[golangci-lint-linters-v1.61.0 sheet][golangci-lint-linters-v1-61-0-sheet]
This sheet helps to manage the configuration for `golangci-lint`.
[golangci-lint-linters-v1-61-0-sheet]: https://docs.google.com/spreadsheets/d/1-pg0JNHOoN8bRer5bRuZFYYPpspYXdwe5wIKjglcvsE/edit?usp=sharing
: Typically, you don't need to frequently update the configuration for golangci-lint. And it is
: quite common to copy and modify configuration from 1 repo to another. But it is still useful to
: have an excel sheet if you are starting from scratch for being able to discuss and gather feedback
: on the proposed configuration. And when I had finished building the sheet, I used some extra time
: I had to generate the config from the sheet itself. So this might be a bit of overengineering.
: --
: The configuration was last updated for version v1.61.0 of golangci-lint.
: The configuration is not perfect for everyone.
: --
: The configuration is updated in a few seconds whenever any change is made in the sheet by the Apps
: Script attached to the project (see triggers.gs).
## Problem: Integrating golangci-lint into a project
**New project**
- No problem
**Pre-existing project**
- Large number of issues could be reported
- _Fixing all the issues in a single effort can be **inhibitively expensive**._
: When we are creating a new project, it is straightforward to integrate golangci-lint into it.
: However, if we are integrating `golangci-lint` into a pre-existing project where either the linter
: was not integrated or the configuration was minimal, we need a strategy to iteratively fix the
: large number of issues that would be reported by the linter with the new/updated configuration.
## Solution: Integrating golangci-lint into an existing project
`golangci-lint` provides a mechanism for gradual adoption by reporting issues only for new and
modified lines in the commit. .
```
$ golangci-lint run --help
Run the linters
Usage:
golangci-lint run [flags]
Flags:
...
--new-from-rev REV Show only new issues created after git revision REV
```
See [golangci-lint FAQ][golangci-lint-faq].
[golangci-lint-faq]: https://golangci-lint.run/welcome/faq/#how-to-integrate-golangci-lint-into-large-project-with-thousands-of-issues
: The official Github action provided by authors of `golangci-lint` also supports a configuration
: option `only-new-issues` which can show only the issues that were introduced by a pull request
: etc.
## Summary
- Linters detect and flag predefined 'patterns'.
- Using linters helps to left shift detections and improves quality and speed of delivery. It helps
bring consistency and improves readability.
- We saw examples that show different scenarios in which linters can catch the issue.
- A bit of extra work to configure `golangci-lint` goes a long way to get the best out of it.
- The configuration builder sheet can help you get started.
- For existing projects, leverage the `--new-from-rev` option provided by `golangci-lint` to fix
issues iteratively.
## Special Thank You
The work done by authors of various linters and `golangci-lint` is immensely valuable and I am very
grateful to them. Thank you!
## Questions?
Talk material:
.image assets/bitly-qr-code.png 350 _
<p style="text-align: center;">
<a href="https://bit.ly/gophercon-in-2024-linters-v1">https://bit.ly/gophercon-in-2024-linters-v1</a>
</p>
[bitly-link]: https://bit.ly/go-mtp-blr-linters-v1