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

PREVIEWING: esther/docs-9518-update-example-control-sensitive-log-data