Use append to concatenate slices
ID: go-best-practices/concatenate-slices
Language: Go
Severity: Info
Category: Best Practices
Description
Programmers should avoid writing for i := range y { x = append(x, y[i]) }
and use x = append(x, y...)
instead.
Here are a few reasons why:
- Simplicity and readability: The
x = append(x, y...)
expression is more concise and clearer in its intent. It directly appends all elements of y
to x
without the need for an explicit loop with index access. It is easier to read and understand for other developers. - Performance: Using the
x = append(x, y...)
syntax is generally faster and more efficient than iterating over each element of y
using a for
loop. The implicit use of variadic arguments in append
reduces memory allocations and improves performance. - Avoiding index access: By using
x = append(x, y...)
, you eliminate the need for manual index access y[i]
and let the built-in append
function handle the internal implementation efficiently. - Consistency: Using
x = append(x, y...)
for concatenation is consistent with other idiomatic Go code. It is a widely accepted practice and is commonly used in the Go community.
By adopting the x = append(x, y...)
approach, programmers can simplify their code, improve performance, and adhere to Go’s idiomatic style. It enhances code readability, reduces the chance of typographical errors, and promotes efficient slicing and concatenation.
Non-Compliant Code Examples
func main() {
for i := range y {
x = append(x, y[i])
}
}
Compliant Code Examples
func main() {
x = append(x, y...)
for _, e := range y {
x = append(x, e)
}
}