Programming Search⌘K

Go lints

The lint rules of Go, with what each one finds. This page has 195 cards.

Apply De Morgan’s laws. QF1001

De Morgan’s laws allow negated boolean expressions to be rewritten by exchanging && and || and negating their operands. For example, !(a && b) can be written as !a || !b. It is in Staticcheck since 2021.1. It is in the quickfix group of checks.

Code
QF1001

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Lift if+break into loop condition. QF1006

An unconditional loop that begins with an if statement whose only action is to break can instead use the negated condition as its loop condition. This makes the loop’s termination condition visible in the loop header and removes a level of nesting. It is in Staticcheck since 2021.1. It is in the quickfix group of checks.

Code
QF1006

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Use fmt.Fprintf(x, ...) instead of x.Write(fmt.Sprintf(...)). QF1012

When writing formatted output to an io.Writer, fmt.Fprint, fmt.Fprintf, and fmt.Fprintln can format and write in one call. This is more direct than passing the result of fmt.Sprint, fmt.Sprintf, or fmt.Sprintln to WriteString or converting it to []byte for Write, and avoids constructing an intermediate string. It is in Staticcheck since 2022.1. It is in the quickfix group of checks.

Code
QF1012

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Simplify returning boolean expression. S1008

An if statement that returns true when a condition holds, followed by a return of false, can be replaced with a single return of the condition. If the return values are reversed, return the negated condition instead. It is in Staticcheck since 2017.1. It is in the simple group of checks.

Code
S1008

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Use a single append to concatenate two slices. S1011

When two slices have the same type, a loop that only appends elements from one to the other can be replaced with a single call to append. Use append(x, y...) to append all elements of y to x, making the concatenation explicit without a loop. It is in Staticcheck since 2017.1. It is in the simple group of checks.

Code
S1011

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Merge variable declaration and assignment. S1021

A variable declaration immediately followed by its initial assignment can be combined into a declaration with an initializer, as long as the initializer does not refer to the variable itself. This keeps the variable and its initial value together while preserving any explicit type needed for the declaration. It is in Staticcheck since 2017.1. It is in the simple group of checks.

Code
S1021

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Range over the string directly. S1029

Ranging over a string will yield byte offsets and runes. If the offset isn’t used, this is functionally equivalent to converting the string to a slice of runes and ranging over that. It is in Staticcheck since 2017.1. It is in the simple group of checks.

Code
S1029

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Elaborate way of sleeping. S1037

Using a select statement with a single case receiving from the result of time.After is a very elaborate way of sleeping that can much simpler be expressed with a simple call to time.Sleep. It is in Staticcheck since 2020.1. It is in the simple group of checks.

Code
S1037

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Type assertion to current type. S1040

The type assertion x.(SomeInterface), when x already has type SomeInterface, can only fail if x is nil. Usually, this is left-over code from when x had a different type and you can safely delete the type assertion. It is in Staticcheck since 2021.1. It is in the simple group of checks.

Code
S1040

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Invalid regular expression. SA1000

The regular expression cannot be parsed by the regexp package. Functions such as regexp.Compile return an error for this pattern, while regexp.MustCompile panics. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA1000

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Invalid template. SA1001

The Parse methods in text/template and html/template return an error for malformed template syntax, such as an unclosed action or an unexpected character in an action. Correct the template’s actions and delimiters before attempting to execute it. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA1001

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Invalid URL in net/url.Parse. SA1007

The URL contains a syntax error that causes net/url.Parse to return an error. Correct the URL’s syntax, using url.PathEscape or url.QueryEscape for individual path segments or query values that require escaping. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA1007

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Non-canonical key in http.Header map. SA1008

Keys in http.Header maps are canonical, meaning they follow a specific combination of uppercase and lowercase letters. Methods such as http.Header.Add and http.Header.Del convert inputs into this canonical form before manipulating the map. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA1008

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Non-pointer value passed to Unmarshal or Decode. SA1014

Functions such as encoding/json.Unmarshal and (*encoding/json.Decoder).Decode require a pointer to the value that should be populated. Passing a non-pointer value results in the function returning an error at runtime, as it cannot modify the target value. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA1014

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Using time.Tick in a way that will leak. Consider using time.NewTicker, and only use time.Tick in tests, commands and endless functions. SA1015

Before Go 1.23, time.Tickers had to be closed to be able to be garbage collected. Since time.Tick doesn’t make it possible to close the underlying ticker, using it repeatedly would leak memory. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA1015

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

It is not possible to use (*time.Timer).Reset’s return value correctly. SA1025

When using pre-Go 1.23 timer semantics, draining a timer’s channel based on Reset’s return value is racy: the new timer may expire before the channel is drained, causing the new notification to be consumed instead of the old one. Stop the timer and, if necessary, drain its channel before calling Reset. It is in Staticcheck since 2019.1. It is in the staticcheck group of checks.

Code
SA1025

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Overlapping byte slices passed to an encoder. SA1031

In an encoding function of the form Encode(dst, src), dst and src were found to reference the same memory. This can result in src bytes being overwritten before they are read, when the encoder writes more than one byte per src byte. It is in Staticcheck since 2024.1. It is in the staticcheck group of checks.

Code
SA1031

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

A value assigned to a variable is never read before being overwritten. Forgotten error check or dead code?. SA4006

A value assigned to a variable may go unused even if the variable is read later, because a later assignment can overwrite the value first. This may hide a missing step, such as checking an error before reusing err. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA4006

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

An if/else if chain has repeated conditions and no side-effects; if the condition didn’t match the first time, it won’t match the second time, either. SA4014

In an if/else if chain without side effects in its conditions, a repeated condition cannot become true after it was already false. The later branch is therefore unreachable. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA4014

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Calling functions like math.Ceil on floats converted from integers doesn’t do anything useful. SA4015

Converting an integer to a floating-point value does not produce a fractional part, NaN, or infinity. Applying math.Ceil, math.Floor, or math.Trunc to that value leaves it unchanged, and math.IsNaN and math.IsInf return false. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA4015

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Self-assignment of variables. SA4018

Assigning a variable to itself preserves its existing value and is usually redundant. It may indicate a typo in either the source or the destination of an assignment, especially in a list of similar field assignments. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA4018

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Multiple, identical build constraints in the same file. SA4019

Multiple legacy +build lines are combined with a logical AND. Repeating the same constraint, even with its space-separated terms in a different order, does not further restrict when the file is built. Remove the duplicate line, or correct it if a different constraint was intended. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA4019

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Impossible comparison of interface value with untyped nil. SA4023

Under the covers, interfaces are implemented as two elements, a type T and a value V. V is a concrete value such as an int, struct or pointer, never an interface itself, and has type T. For instance, if we store the int value 3 in an interface, the resulting interface value has, schematically, (T=int, V=3). It is in Staticcheck since 2020.2. It is in the staticcheck group of checks.

Code
SA4023

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

X % 1 is always zero. SA4028

The remainder of integer division by one is always zero, regardless of the dividend. If a remainder was intended, check whether the divisor should be a different value. It is in Staticcheck since 2022.1. It is in the staticcheck group of checks.

Code
SA4028

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Ineffective attempt at generating random number. SA4030

Functions in the math/rand package that accept upper limits, such as Intn, generate random numbers in the half-open interval [0,n). In other words, the generated numbers will be >= 0 and < n – they don’t include n. rand.Intn(1) therefore doesn’t generate 0 or 1, it always generates 0. It is in Staticcheck since 2022.1. It is in the staticcheck group of checks.

Code
SA4030

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Comparing runtime.GOOS or runtime.GOARCH against impossible value. SA4032

A file’s build constraints, including those implied by its name, limit the platforms on which its code can run. Comparing runtime.GOOS or runtime.GOARCH with a value excluded by those constraints makes an equality check always false and an inequality check always true. It is in Staticcheck since 2024.1. It is in the staticcheck group of checks.

Code
SA4032

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

The empty for loop (for {}) spins and can block the scheduler. SA5002

An empty loop with no condition runs continuously, consuming CPU without doing useful work. An empty loop whose condition only reads ordinary variables cannot safely wait for another goroutine to change them: unsynchronized reads and writes constitute a data race. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA5002

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Invalid struct tag. SA5008

Malformed struct tags, duplicate keys, and invalid JSON or XML options can cause reflection-based packages to ignore the intended settings or report errors. Use the conventional space-separated key:"value" syntax and ensure that each option is supported by the package reading the tag and is appropriate for the field’s type. It is in Staticcheck since 2019.2. It is in the staticcheck group of checks.

Code
SA5008

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Invalid Printf call. SA5009

Formatting functions such as fmt.Printf and fmt.Sprintf require the format string to agree with the number and types of the arguments. Malformed formats, invalid argument indices, and mismatched arguments produce diagnostic text such as %!d(string=...) instead of the intended output. It is in Staticcheck since 2019.2. It is in the staticcheck group of checks.

Code
SA5009

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Impossible type assertion. SA5010

Some type assertions can be statically proven to be impossible. This is the case when the method sets of both arguments of the type assertion conflict with each other, for example by containing the same method with different signatures. It is in Staticcheck since 2020.1. It is in the staticcheck group of checks.

Code
SA5010

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Using a non-octal os.FileMode that looks like it was meant to be in octal. SA9002

File permissions are conventionally written in octal, with one digit for each of the owner, group, and other permission sets. A literal such as 644 is decimal in Go and sets different permission bits than intended, potentially granting unwanted access. It is in Staticcheck since 2017.1. It is in the staticcheck group of checks.

Code
SA9002

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Deleting a directory that shouldn’t be deleted. SA9007

It is virtually never correct to delete system directories such as /tmp or the user’s home directory. However, it can be fairly easy to do by mistake, for example by mistakenly using os.TempDir instead of ioutil.TempDir, or by forgetting to add a suffix to the result of os.UserHomeDir. It is in Staticcheck since 2022.1. It is in the staticcheck group of checks.

Code
SA9007

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Don’t use Yoda conditions. ST1017

Yoda conditions are conditions of the kind if 42 == x, where the literal is on the left side of the comparison. These are a common idiom in languages in which assignment is an expression, to avoid bugs of the kind if (x = 42). It is in Staticcheck since 2019.2. It is in the stylecheck group of checks.

Code
ST1017

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.

Redundant type in variable declaration non-default. ST1023

In a local variable declaration, omit an explicit type when the initializer already determines the same type. For example, use var x = f() instead of var x T = f() when f returns T. Keep the explicit type when omitting it would change the variable’s type, such as when assigning a concrete value to an interface variable. It is in Staticcheck since 2021.1. It is in the stylecheck group of checks.

Code
ST1023

Source: staticcheck.dev. JSON record. This entry is a card. Help write this page.