Map keys must be comparable, which precludes the use of byte slices. This
usually leads to using string keys and converting byte
slices to strings.
regexp.Match
or related in a loop SCC-SA6000Compile parses a regular expression and returns, if successful, a Regexp object that can be used to match against text.
When matching in a loop, use regexp.Compile
instead.
time.Tick
used in leaky way SCC-SA1015Using time.Tick
in a way that will leak. Consider using time.NewTicker, and
only use time.Tick in tests, commands and endless functions.
sync.Pool
allocates memory SCC-SA6002A sync.Pool
is used to avoid unnecessary allocations and reduce the amount of
work the garbage collector has to do.
Loop directly over the runes in a string instead of converting the string to a
slice of rune
and then looping over the same.
string
comparison with strings.ToLower
or strings.ToUpper
SCC-SA6005Instead of using strings.ToLower
/strings.ToUpper
with equality check for
string comparison, use strings.EqualFold
directly.
append
can be combined into a single call CRT-P0001Multiple calls for append
could be combined into a single call for append
.
When a param big in size (more than 80 bytes) is passed to another function, it is better to pass a pointer to the value around, rather than the value itself.
strings.Index
call cause unwanted allocations CRT-P0004[]byte
to string
conversion is required when passing the arguments to
strings.Index
, leading to unwanted allocations. It is preferred to use calls
that prevent (or minimize) such allocations (e.g., bytes.Index
, etc.).
range
CRT-P0005Use pointer to slice
or array
, to avoid copying the value that is ranged
over.
Avoid for
loop range expressions that copy big objects for each iteration.
It is recommended to use index-based access or use pointers instead.
Sometimes reordering the operands can save computational resources because of the properties of operators like "&&" (AND) or "||" (OR) in a binary expression.
For logical operators ("AND" and "OR"), if the evaluation of an expression exits before complete evaluation, it is known as "short-circuiting." A short circuit happens when the result is clear even before the complete evaluation of the expression, and the result is returned, avoiding unnecessary evaluation and leading to efficient processing.
for
loop GO-P4001Using non-idiomatic slice zeroing might not get recognized by the compiler for further optimizations. Using the idiomatic slice zeroing method is recommended so the compiler can optimize it during compilation.
WriteByte
over WriteRune
for byte literals GO-P4005It is recommended to use WriteByte
instead of WriteRune
for writing byte
literals. WriteRune
appends the UTF-8
encoding of unicode code point to the
buffer.
utf8.DecodeRuneInString
instead of []rune(string)[0]
GO-P4006It is recommended to use utf8.DecodeRuneInString
instead of
[]rune(string)[0]
to prevent unwanted rune
slice allocation.
fmt.Fprint
instead of (io.Writer).Write
along with fmt.Sprint
GO-P4007It is recommended to use fmt.Fprint
(and friends) instead of writing the
result of an fmt.Sprint
(and friends) call to write to an io.Writer
. This
reduces the number of allocations required, therefore improving performance.
(io.StringWriter).WriteString
for writing strings GO-P4008It is recommended to use (io.StringWriter).WriteString
instead of
(io.StringWriter).Write
or io.WriteString
for writing strings as it decreases
the number of allocations required, therefore improving performance.