How To Clean The Lint Trap On The Ge Cv H680 Drive
A guide to linting Go programs
Linting is the process of identifying and reporting on patterns found in code, with the aim of improving consistency, and communicable bugs early in the development bicycle. This is specially useful when working in a team as information technology helps to make all code expect the same no matter who's writing it, which reduces complication, and makes the code easier to maintain. In this article, I'll demonstrate a comprehensive linting setup for Become programs, and talk about the best way to introduce it into an existing projection.
Linting code is one of the most basic things you can do to ensure consistent coding practices in project. Go already ventures farther than nigh other programming languages past bundling gofmt, a formatting tool that ensures that all Get code looks the same, just it just deals with how code is beingness formatted. The become vet tool is also available to assistance with detecting suspicious constructs that may not be defenseless by the compile, but it only catches a limited amount of potential bug.
The task of developing more comprehensive linting tools has been left to the wider community, and this has yielded a mountain of linters, each one with a specific purpose. Prominent examples include:
- unused - Checks Get code for unused constants, variables, functions and types.
- goconst - Find repeated strings that could be replaced past a constant.
- gocyclo - Computes and checks the cyclomatic complication of functions.
- errcheck - Detect unchecked errors in Go programs.
The problem with having and then many standalone linting tools is that you have to download each private linter yourself and manage their versions. Additionally, running each 1 of them in sequence may be besides slow. Due to these reasons, golangci-lint, a Go linters aggregator that runs linters in parallel, reuses the Go build cache, and caches analysis results for much improved performance on subsequent runs, is the preferred manner to setup linting in Go projects.
The golangci-lint projection was adult to aggregate and run several individual linters in parallel for convenience and performance reasons. When y'all install the program, yous'll get virtually 48 linters included (at the time of writing), and you can proceed to pick and cull which ones are important for your project. Aside from running information technology locally during development, y'all are able to set up it upwards as part of your continuous integration workflow.
Installing golangci-lint
Apply the command below to install golangci-lint locally on any operating organisation. Other OS-specific installation options can be found here.
$ become install github.com/golangci/golangci-lint/cmd/golangci-lint@latest In one case installed, you should cheque the version that was installed:
$ golangci-lint version golangci-lint has version v1.40.1 built from (unknown, mod sum: "h1:pBrCqt9BgI9LfGCTKRTSe1DfMjR6BkOPERPaXJYXA6Q=" ) on (unknown) Y'all can also view the all the available linters through the following command:
$ golangci-lint help linters
The vast majority of bachelor linters are disabled by default
If you run the enabled linters at the root of your projection directory, you may encounter some errors. Each trouble is reported with all the context you demand to gear up it including a short clarification of the issue, and the file and line number where information technology occurred.
$ golangci-lint run # equivalent of golangci-lint run ./...
golangci-lint provides a squeamish output with colors, source lawmaking lines and marked identifiers
You can also choose which directories and files to analyse by passing one or more directories or paths to files.
$ golangci-lint run dir1 dir2 dir3/chief.go Configuring golangci-lint
GolangCI-Lint is designed to be as flexible as possible for a wide range of use cases. The configuration for golangci-lint tin be managed through command line options or a configuration file, although the former has a greater priority over the latter if both are used at the same time. Hither'south an example that uses command-line options to disable all linters and configure the specific linters that should be run:
$ golangci-lint run --disable-all -East revive -Eastward errcheck -E nilerr -E gosec You can likewise run the presets provided by golangci-lint. Hither'due south how to find out about the available presets:
$ golangci-lint aid linters | sed -n '/Linters presets:/,$p' Linters presets: bugs: asciicheck, bodyclose, durationcheck, errcheck, errorlint, exhaustive, exportloopref, gosec, govet, makezero, nilerr, noctx, rowserrcheck, scopelint, sqlclosecheck, staticcheck, typecheck annotate: godot, godox, misspell complexity: cyclop, funlen, gocognit, gocyclo, nestif fault: errcheck, errorlint, goerr113, wrapcheck format: gci, gofmt, gofumpt, goimports import: depguard, gci, goimports, gomodguard metalinter: gocritic, govet, revive, staticcheck module: depguard, gomoddirectives, gomodguard functioning: bodyclose, maligned, noctx, prealloc sql: rowserrcheck, sqlclosecheck style: asciicheck, depguard, dogsled, dupl, exhaustivestruct, forbidigo, forcetypeassert, gochecknoglobals, gochecknoinits, goconst, gocritic, godot, godox, goerr113, goheader, golint, gomnd, gomoddirectives, gomodguard, goprintffuncname, gosimple, ifshort, importas, interfacer, lll, makezero, misspell, nakedret, nlreturn, nolintlint, paralleltest, predeclared, promlinter, revive, stylecheck, tagliatelle, testpackage, thelper, tparallel, unconvert, wastedassign, whitespace, wrapcheck, wsl exam: exhaustivestruct, paralleltest, testpackage, tparallel unused: deadcode, ineffassign, structcheck, unparam, unused, varcheck Then y'all can run a preset by passing its proper noun to the --preset or -p flag:
$ golangci-lint run -p bugs -p error Configuring golangci-lint for a project is best done through a configuration file. That fashion, yous'll be able to configure specific linter options which is not possible via command-line options. Yous may specify the configuration file in YAML, TOML or JSON format, merely I recommend sticking with the YAML format (.golangci.yml or .golangci.yaml) since that's what is used on the official documentation pages.
Generally speaking, you should create projection-specific configuration in the root of your projection directory. The program will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This ways you can achieve a global configuration for all projects by placing a config file in your domicile directory (not recommended). This file will be used if a locally scoped config file does not exist.
A sample configuration file is available on the golangci-lint website with all supported options, their description, and default value. Yous can utilise that every bit a starting point when creating your own configuration. Keep in mind that some linters perform similar functions so you need to enable linters deliberately to avert redundant entries. Here's the general configuration that I use for my personal projects:
linters-settings : errcheck : bank check-type-assertions : true goconst : min-len : 2 min-occurrences : 3 gocritic : enabled-tags : - diagnostic - experimental - opinionated - performance - way govet : check-shadowing : true nolintlint : require-explanation : true crave-specific : truthful linters : disable-all : true enable : - bodyclose - deadcode - depguard - dogsled - dupl - errcheck - exportloopref - exhaustive - goconst - gocritic - gofmt - goimports - gomnd - gocyclo - gosec - gosimple - govet - ineffassign - misspell - nolintlint - nakedret - prealloc - predeclared - revive - staticcheck - structcheck - stylecheck - thelper - tparallel - typecheck - unconvert - unparam - varcheck - whitespace - wsl run : issues-exit-code : ane Suppressing linting errors
It's sometimes necessary to disable specific linting bug that crop upward in a file or package. This may be achieved in two chief means: through the nolint directive, and through exclusion rules in the configuration file. Permit'due south take a look at each approach in turn.
The nolint directive
Let's assume nosotros have the following lawmaking that prints a pseudo random integer to the standard output:
package main import ( "fmt" "math/rand" "time" ) func primary () { rand . Seed ( fourth dimension . Now (). UnixNano ()) fmt . Println ( rand . Int ()) } Executing golangci-lint run on this file will produce the following error provided that the gosec linter is enabled:
$ golangci-lint run -Eastward gosec master.go:eleven:fourteen: G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec) fmt.Println(rand.Int()) ^ The linter is encouraging the utilize of the Int method from crypto/rand instead because it is cryptographically more than secure, but information technology has the tradeoff of a less friendly API and slower performance. If you're OK with the tradeoff of less secure pseudo random numbers for faster speeds, you can ignore the error by adding a nolint directive on the necessary line:
func main () { rand . Seed ( time . Now (). UnixNano ()) fmt . Println ( rand . Int ()) //nolint } This inline usage of nolint causes all the linting problems detected for that line to exist disabled. You tin can disable the issues from a specific linter by specifying its name in the directive (recommended). This allows bug raised on that line by other linters to come through.
func primary () { rand . Seed ( time . Now (). UnixNano ()) fmt . Println ( rand . Int ()) //nolint:gosec } When you lot use a nolint directive at the top of a file, information technology disables all the linting issues for that file:
//nolint:govet,errcheck package principal You lot can also exclude issues for a block of code (such equally a function), by using a nolint directive at the first of the block.
//nolint func aFunc () { } After adding a nolint directive, it is recommended that you add a comment explaining why it is needed. This annotate should be placed on the same line every bit the flag itself:
func main () { rand . Seed ( time . At present (). UnixNano ()) fmt . Println ( rand . Int ()) //nolint:gosec // for faster performance } Yous can enforce the conventions that your team should follow regarding nolint comments by enabling the nolintlint linter. It can report issues regarding the use of nolint without naming the specific linter being suppressed, or without a comment explaining why it was needed.
$ golangci-lint run main.become:11:26: directive `//nolint` should mention specific linter such as `//nolint:my-linter` (nolintlint) fmt.Println(rand.Int()) //nolint ^ Exclusion rules
Exclusion rules tin be specified in the configuration file for a more than granular control on what files are linted, and what bug are reported. For example, you tin disable certain linters from running on test files (_test.go), or you can disable a linter from producing sure errors projection-wide:
issues : exclude-rules : - path : _test\.become # disable some linters on exam files linters : - gocyclo - gosec - dupl # Exclude some gosec messages project-broad - linters : - gosec text : "weak cryptographic primitive" Integration with existing projects
When adding golangci-lint to an existing project, yous may get a lot of issues and it may be difficult to prepare all of them at once. Notwithstanding, that doesn't mean that you should abandon the idea of linting your project for this reason. There is a new-from-rev setting that allows you to prove merely new problems created after a specific git revision which makes it easy to lint new code only until adequate fourth dimension can be budgeted to set older issues. Once you discover the revision you want to outset linting from (with git log), you can specify it in your configuration file as follows:
bug : # Testify only new issues created after git revision: 02270a6 new-from-rev : 02270a6 Integrating golangci-lint in your editor
GolangCI-Lint supports integrations with several editors in guild to get quick feedback. In Visual Studio Code, all you demand to do is install the Go extension, and add the post-obit lines to your settings.json file:
{ "go.lintTool" : "golangci-lint" , "go.lintFlags" : [ "--fast" ] }
Vim users can integrate golangci-lint with a variety of plugins including vim-go, ALE, and Syntastic. Y'all tin can also integrate it with coc.nvim, vim-lsp, or nvim.lspconfig with help of golangci-lint-langserver. Here's how I integrated golangci-lint in my editor with coc.nvim. Commencement, install the linguistic communication server:
$ go install github.com/nametake/golangci-lint-langserver@latest Next, open up the coc.nvim config file with :CocConfig, and add the post-obit lines:
{ "languageserver" : { "golangci-lint-languageserver" : { "control" : "golangci-lint-langserver" , "filetypes" : [ "get" ], "initializationOptions" : { "control" : [ "golangci-lint" , "run" , "--out-format" , "json" ] } } } } Salvage the config file, so restart coc.nvim with :CocRestart, or open up a new case of Vim. It should starting time working every bit soon as a Go file is open in the editor.
voila
Refer to the golangci-lint docs for more than information on how to integrate it with other editors.
Setting up a pre-commit hook
Image source
Running golangci-lint as office of your Git pre-commit hooks is a corking manner to ensure that all Go code that is checked into source control is linted properly. If you lot haven't set up a pre-commit claw for your project, here's how to ready ane upwards with pre-commit, a language-agnostic tool for managing Git claw scripts.
Install the pre-commit packet manager by following the instructions on this page, then create a .pre-commit-config.yaml file in the root of your projection, and populate it with the following contents:
repos : - repo : https://github.com/tekwizely/pre-commit-golang rev : v0.8.three # change this to the latest version hooks : - id : golangci-lint This configuration file extends the pre-commit-golang repository which supports various hooks for Go projects. The golangci-lint claw targets staged files only, which is handy for when introducing golangci-lint to an existing project so that you don't get overwhelmed with so many linting issues at once. Once you've saved the file, run pre-commit install to gear up the git hook scripts in the current repository.
$ pre-commit install pre-commit installed at .git/hooks/pre-commit On subsequent commits, the specified hooks will run on all staged .go files and halt the committing process if errors are discovered. Yous'll demand to prepare all the linting bug earlier you'll be allowed to commit. Y'all can too utilise the pre-commit run command if yous want to test the pre-commit hook without making a commit.
Continuous Integration (CI) workflow
Running your project's linting rules on each pull request prevents lawmaking that is not upwards to standards from slipping through into your codebase. This can also be automated by adding golangci-lint to your Continuous Integration process. If you use GitHub Deportment, the official Action should exist preferred over a unproblematic binary installation for performance reasons. After setting it upwardly, you'll get an inline display of whatsoever reported issues on pull requests.
During the setup process, ensure to pin the golangci-lint version that is being used so that information technology yields consistent results with your local environment. The project is being actively developed, so updates may deprecate some linters, or report more errors than previously detected for the aforementioned source lawmaking.
Conclusion
Linting your programs is a sure fire way to ensure consistent coding practices amidst all contributors to a project. Past adopting the tools and processes discussed in this commodity, yous'll be well on your way to doing but that.
Cheers for reading, and happy coding!
Source: https://freshman.tech/linting-golang/
Posted by: evansagetaing93.blogspot.com

0 Response to "How To Clean The Lint Trap On The Ge Cv H680 Drive"
Post a Comment