Do not use append for assignment

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: go-best-practices/equivalent-append

Language: Go

Severity: Warning

Category: Best Practices

Description

In Go, the append() function is used to append elements to a slice. It is a built-in function that takes a slice and one or more elements as arguments and returns a new slice with the appended elements.

When append() is called with a single argument, it is similar to the assignment of that argument to a variable. This is because the append() function returns a new slice that includes the original elements of the input slice and the appended element(s).

For example:

s := []int{1, 2, 3}
s = append(s, 4)

In this code snippet, the append() function is called with a single argument s and the value 4. The append() function returns a new slice with the elements [1, 2, 3, 4], and this new slice is assigned back to the variable s. The original value of s is replaced with the new slice containing the appended element.

It is important to note that the append() function does not modify the original slice in-place since slices are reference types in Go. Instead, it returns a new slice that combines the elements of the original slice and the appended element(s).

In summary, when append() is used with a single argument, it effectively performs an assignment of the argument to the variable, replacing the original value with a new slice that includes the appended element(s).

Non-Compliant Code Examples

func main() {
    x = append(y)
}

Compliant Code Examples

func main() {
    x = y
    gitdbClientValue = getGitDBClient(api)
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: mcretzman/DOCS-9337-add-cloud-info-byoti