PQ not maintained anymore : “github.com/lib/pq”
SQLC – SQL compilers
Masterminds/Squirrel
Database/Sql
Jackc/pgx
Linter is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs.
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
go vet -vettool=$(which shadow)
There are more than 50 linters in Go programming language. However, only few are useful. Most common way to use linters in open source project is to use some subset of linters. For example: go vet, errcheck, and golint are popular linters and are really cool.
Go Tools
gofmt
goimports
Complexity Check
- Gocyclo
- It computes and checks the cyclomatic complexity of functions. For example, the following code.
- Gocyclo calculates cyclomatic complexities of functions in Go source code.
Cyclomatic complexity is a code quality metric which can be used to identify code that needs refactoring. It measures the number of linearly independent paths through a function’s source code.
The cyclomatic complexity of a function is calculated according to the following rules:
1 is the base complexity of a function
+1 for each ‘if’, ‘for’, ‘case’, ‘&&’ or ‘||’
A function with a higher cyclomatic complexity requires more test cases to cover all possible paths and is potentially harder to understand. The complexity can be reduced by applying common refactoring techniques that lead to smaller functions.
https://github.com/fzipp/gocyclo
- Nakedret
- It finds naked returns in functions greater than a specified function length.
- https://github.com/alexkohler/nakedret
Style Check
- gochecknoglobals
It checks that no globals are present in Go code. - golint
Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes. - gosimple
It specializes in simplifying a code. - goconst
It finds repeated strings that could be replaced by a constant. - misspell
It finds commonly misspelled English words in comments. - unconvert
It removes unnecessary type conversions.
Unused code check
- varcheck
It finds unused global variables and constants. - unused
It checks Go code for unused constants, variables, functions and types. - deadcode
It simply finds unused code. - unparam
It reports unused function parameters. - ineffassign
It detects when assignments to existing variables are not used.
Performance Check
- maligned
It is a tool to detect Go structs that would take less memory if their fields were sorted. - gocritic
The most opinionated Go source code linter. - prealloc
It finds slice declarations that could potentially be preallocated.
Bug Finding
- scopelint
Scopelint checks for unpinned variables in go programs. - staticcheck
Staticcheck is a go vet on steroids, applying a ton of static analysis checks. - gosec
It inspects source code for security problems. - errcheck
Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases. - gometalinter
It is an open source linters runner. It aggregates all linters in once and allow us to run all the linters with just one line of code. It is useful, runs parallel and much more efficient than xargs. - golangci-lint
GolangCI-Lint is a linters aggregator. It’s fast: on average 5 times faster than gometalinter. It’s easy to integrate and use, has nice output and has a minimum number of false positives.