golang packages and tools

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

Style Check

  1. gochecknoglobals
    It checks that no globals are present in Go code.
  2. golint
    Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes.
  3. gosimple
    It specializes in simplifying a code.
  4. goconst
    It finds repeated strings that could be replaced by a constant.
  5. misspell
    It finds commonly misspelled English words in comments.
  6. unconvert
    It removes unnecessary type conversions.

Unused code check

  1. varcheck
    It finds unused global variables and constants.
  2. unused
    It checks Go code for unused constants, variables, functions and types.
  3. deadcode
    It simply finds unused code.
  4. unparam
    It reports unused function parameters.
  5. ineffassign
    It detects when assignments to existing variables are not used.

Performance Check

  1. maligned
    It is a tool to detect Go structs that would take less memory if their fields were sorted.
  2. gocritic
    The most opinionated Go source code linter.
  3. prealloc
    It finds slice declarations that could potentially be preallocated.

Bug Finding

  1. scopelint
    Scopelint checks for unpinned variables in go programs.
  2. staticcheck
    Staticcheck is a go vet on steroids, applying a ton of static analysis checks.
  3. gosec
    It inspects source code for security problems.
  4. errcheck
    Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases.
  5. 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.
  6. 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.

https://freshman.tech/linting-golang/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s