strings.Replace with 0 does not do anything

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

Metadata

ID: go-best-practices/strings-replace-zero

Language: Go

Severity: Info

Category: Best Practices

Description

The code strings.Replace(str, something, somethingElse, 0) is considered bad practice because it won’t actually replace anything in the string str.

In Go’s strings.Replace function, the last argument n represents the maximum number of replacements to be made. When n is set to 0, the function doesn’t perform any replacements, and the original string str remains unchanged.

Therefore, if the intention is to replace occurrences of something with somethingElse in the string str, setting n to 0 will prevent any replacements from occurring.

To correctly replace occurrences in a string, the value of n should be set to a positive integer that indicates the maximum number of replacements to be made, or -1 to replace all occurrences.

For example, to replace all occurrences of something with somethingElse, the code should be modified as follows:

strings.Replace(str, something, somethingElse, -1)

By setting n to -1, the strings.Replace function will replace all instances of something with somethingElse in the string str.

Using the appropriate value for n ensures that the replacements are performed as intended and the desired behavior is achieved. It is important to read the function documentation and understand the purpose and behavior of each parameter to avoid unexpected results in your code.

Non-Compliant Code Examples

func main() {
    strings.Replace(str, something, somethingElse, 0)
}

Compliant Code Examples

func main() {
    strings.Replace(str, something, somethingElse, -1)
}
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