• Stars
    star
    2,329
  • Rank 19,770 (Top 0.4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 11 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

errcheck checks that you checked errors.

errcheck

errcheck is a program for checking for unchecked errors in Go code.

errcheck

Install

go install github.com/kisielk/errcheck@latest

errcheck requires Go 1.18 or newer.

Use

For basic usage, just give the package path of interest as the first argument:

errcheck github.com/kisielk/errcheck/testdata

To check all packages beneath the current directory:

errcheck ./...

Or check all packages in your $GOPATH and $GOROOT:

errcheck all

errcheck also recognizes the following command-line options:

The -tags flag takes a space-separated list of build tags, just like go build. If you are using any custom build tags in your code base, you may need to specify the relevant tags here.

The -asserts flag enables checking for ignored type assertion results. It takes no arguments.

The -blank flag enables checking for assignments of errors to the blank identifier. It takes no arguments.

The -abspath flag prints the absolute paths to files with unchecked errors.

The -mod flag sets the module download mode to use: readonly or vendor.

go/analysis

The package provides Analyzer instance that can be used with go/analysis API.

Currently supported flags are blank, assert, exclude, and excludeonly. Just as the API itself, the analyzer is experimental and may change in the future.

Excluding functions

Use the -exclude flag to specify a path to a file containing a list of functions to be excluded.

errcheck -exclude errcheck_excludes.txt path/to/package

The file should contain one function signature per line. The format for function signatures is package.FunctionName while for methods it's (package.Receiver).MethodName for value receivers and (*package.Receiver).MethodName for pointer receivers. If the function name is followed by string of form (TYPE), then the the function call is excluded only if the type of the first argument is TYPE. It also accepts a special suffix (os.Stdout) and (os.Stderr), which excludes the function only when the first argument is a literal os.Stdout or os.Stderr.

An example of an exclude file is:

io.Copy(*bytes.Buffer)
io.Copy(os.Stdout)
os.ReadFile

// Sometimes we don't care if a HTTP request fails.
(*net/http.Client).Do

By default, the exclude list is combined with an internal list for functions in the Go standard library that have an error return type but are documented to never return an error. To disable the built-in exclude list, pass the -excludeonly flag.

Run errcheck in -verbose mode to see the resulting list of added excludes.

When using vendored dependencies, specify the full import path. For example:

  • Your project's import path is example.com/yourpkg
  • You've vendored example.net/fmt2 as vendor/example.net/fmt2
  • You want to exclude fmt2.Println from error checking

In this case, add this line to your exclude file:

example.com/yourpkg/vendor/example.net/fmt2.Println

Empty lines and lines starting with // are ignored.

The deprecated method

The -ignore flag takes a comma-separated list of pairs of the form package:regex. For each package, the regex describes which functions to ignore within that package. The package may be omitted to have the regex apply to all packages.

For example, you may wish to ignore common operations like Read and Write:

errcheck -ignore '[rR]ead|[wW]rite' path/to/package

or you may wish to ignore common functions like the print variants in fmt:

errcheck -ignore 'fmt:[FS]?[Pp]rint*' path/to/package

The -ignorepkg flag takes a comma-separated list of package import paths to ignore:

errcheck -ignorepkg 'fmt,encoding/binary' path/to/package

Note that this is equivalent to:

errcheck -ignore 'fmt:.*,encoding/binary:.*' path/to/package

If a regex is provided for a package pkg via -ignore, and pkg also appears in the list of packages passed to -ignorepkg, the latter takes precedence; that is, all functions within pkg will be ignored.

Note that by default the fmt package is ignored entirely, unless a regex is specified for it. To disable this, specify a regex that matches nothing:

errcheck -ignore 'fmt:a^' path/to/package

The -ignoretests flag disables checking of _test.go files. It takes no arguments.

The -ignoregenerated flag disables checking of generated source code. It takes no arguments.

Exit Codes

errcheck returns 1 if any problems were found in the checked files. It returns 2 if there were any other failures.

Editor Integration

Emacs

go-errcheck.el integrates errcheck with Emacs by providing a go-errcheck command and customizable variables to automatically pass flags to errcheck.

Vim

vim-go can run errcheck via both its :GoErrCheck and :GoMetaLinter commands.

More Repositories

1

godepgraph

A Go dependency graph visualization tool
Go
1,028
star
2

sqlstruct

sqlstruct provides some convenience functions for using structs with go's database/sql package
Go
173
star
3

vigo

Vi in Go
Go
168
star
4

gostatsd

An implementation of Etsy's statsd in Go
91
star
5

raven-go

raven-go is a Go client for Sentry
Go
64
star
6

gotool

A library of some of the utility functions provided by (but not exported) by cmd/go
Go
62
star
7

og-rek

ogΓ³rek is a Go library for encoding and decoding pickles.
Go
61
star
8

whisper-go

A golang library for whisper databases
Go
40
star
9

vendorize

vendorize is a tool for vendorizing go imports, including transitive dependencies
Go
20
star
10

goflamegraph

FlameGraphs for Go
Go
17
star
11

covenant

Code contracts for Python 3
Python
17
star
12

takingoutthetrash

Taking Out the Trash - Optimizing Memory Allocation in Go Programs. A talk from the Feb 3, 2015 Golang Vancouver meetup.
11
star
13

mutator

Performs mutations on Go code
Go
10
star
14

jsonrpc-example

Example of using Go's JSON-RPC from other languages.
Go
8
star
15

gorge

gorge is a Go library for interacting with GridEngine
Go
8
star
16

passpersist

A library for writing Net-SNMP pass_persist commands
Python
7
star
17

cmd

A package for creating line-oriented interactive interpreters
Go
5
star
18

goxmind

A Go library for working with XMind files.
Go
5
star
19

bigset

Package bigset implements a set type for storing non-negative integers using the Go standard library's math/big package.
Go
4
star
20

monome

Go library for Monome
Go
3
star
21

dotvim

My Vim configuration.
Vim Script
3
star
22

build-web-application-with-golang_EN

This is an English version of γ€ŠGo Web编程》, which original version was written by AstaXie and translated by Unknown.
2
star
23

gojunit

JUnit XML formatter for Go test output
Go
2
star
24

goversiontags

A demonstration of go version tags
Go
2
star
25

robota

JavaScript
1
star
26

spirytus

spirytus rektyfikowany
Go
1
star
27

gosrc

gosrc.org
Go
1
star
28

latexmath2png

Automatically exported from code.google.com/p/latexmath2png
Python
1
star
29

shapeshifter

Go
1
star
30

set

set is a Go library for working with finite sets
Go
1
star
31

flagcxx

A header-only flag parsing library for C++17 and above, inspired by the Go flag package.
C++
1
star