.Close
method GO-S2307Calling Close()
method may return an error, and ignoring the same
might result in a data loss. This is similar to many more Close
methods.
(*mail.Address).String()
instead of fmt.Sprintf
for mail address GO-W1031For a name and address that is supposed to formatted to a single mail address, it is recommended
to use net/mail's (*Address).String
instead of manually formatting using fmt.Sprintf
which
makes the code much more reliable.
t.Setenv
and friends instead of os.Setenv
for test file(s) GO-W1032For tests, environment variables are sometimes temporarily set. It is also essential and
even a good practice to unset those temporary values as they might interfere with others
trying to use the same environment variable. It is recommended that for *_test.go
file(s)
one should use Setenv
method from the "testing" package as it unsets the same after the
test has concluded. Following Setenv
calls os.Setenv(key, value)
and uses Cleanup
to
restore the environment variable to its original value after the test.
Printf
-like function without f
suffix GO-W6006For Printf
-like functions they should be named correctly so that consumers of the function
use it correctly. It is recommended to use f
as suffix for such functions to make it clear
that arguments are preceded with format specifier.
t.Parallel()
method GO-W6007Using the t.Parallel() method can be tricky and ollowing are some common inappropriate
usages of t.Parallel
:
— t.Parallel()
is called in either a top-level test function or a sub-test function only
— t.Parallel()
is called in the sub-test function; it is post-processed by defer
instead of t.Cleanup()
Here's a good blog post explaining how t.Parallel
should be used.
Defers are scoped to the surrounding function, not the surrounding block. In a function that never returns, i.e. one containing an infinite loop, defers will never execute.
for {}
) spins and can block the scheduler SCC-SA5002An empty loop is bad news in two cases: 1) The loop has no condition. In that case, it's just a loop that spins forever and as fast as it can, keeping a core busy.
Useless assignments are usually a result of error. For example:
A net.IP stores an IPv4 or IPv6 address as a slice of bytes. The length of the slice for an IPv4 address, however, can be either 4 or 16 bytes long, using different ways of representing IPv4 addresses.
Issue is raised when templates cannot be parsed by the Parse
function of html/template
or text/template
. For example, {{.Name}} {{.LastName}
can not be parsed and causes runtime errors.
host:port
pair with net.Listen
SCC-SA1020Valid host:port
pair should be of format hostname:n
where n >= 0 && n <= 65535. Passing invalid host:port
pair causes bug risk and potential security exposure.
Generally, NaN is not considered equal to any number, including itself. That's because it represnts a number outside the range of representation.
A function that calls itself recursively needs to have an exit condition. Otherwise it will recurse forever, until the system runs out of memory.
A function that calls itself recursively needs to have an exit condition. Otherwise it will recurse forever, until the system runs out of memory.
Attempts to pass a Go chan, map, func, or slice to C, either directly, or via a pointer, array, or struct is not recommended. This is because the values of these types (apart from zero-value) always contain Go pointers, which is not
strings.Replace
/ bytes.Replace
called with n == 0
, which does nothing SCC-SA1018With n == 0
, zero instances will be replaced. To replace all instances, use a negative number, or use strings.ReplaceAll
/ bytes.ReplaceAll
.
time.Timer.Reset()
's return value correctly SCC-SA1025It is not possible to use Reset's return value correctly, as there is a race condition between draining the channel and the new timer expiring. Reset should always be invoked on stopped or expired channels. The return value exists to preserve compatibility with existing programs.
On ARM, x86-32, and 32-bit MIPS, the caller's responsibility is to arrange for 64-bit alignment of 64-bit words accessed atomically. The first word in a variable or an allocated struct, array, or slice can be relied upon to be
testing.T.FailNow
or testing.T.SkipNow
in a goroutine SCC-SA2002Calling t.Testing.FailNow
or t.Testing.SkipNow
in a goroutine isn't allowed.
Comparing unsigned values against negative values is pointless. This expression will always be true, because unsigned integers will never be less than zero.