Do not use strings.Split[After]N with negative limit

이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: go-best-practices/strings-splitn

Language: Go

Severity: Warning

Category: Best Practices

Description

In Go, the strings.SplitN() function is used to split a string into substrings based on a delimiter. The function takes in two parameters: the original string to be split and the delimiter string. Additionally, it can take an optional integer limit parameter, which determines the number of substrings to be returned.

Calling strings.SplitN() with a negative limit can lead to unexpected behavior and potential errors in your code. According to the Go documentation, if the limit is negative, there is no limit on the number of substrings returned. This means that it will split the original string into as many substrings as possible, resulting in an unpredictable number of substrings.

For example:

fmt.Println(strings.SplitN("Hello,World,Welcome", ",", -1))

In this example, SplitN will split the string “Hello,World,Welcome” at every occurrence of the delimiter “,” and return all substrings. The resulting substrings will be [“Hello”, “World”, “Welcome”].

To avoid using a negative limit with strings.SplitN(), it is recommended to use a positive limit or to use the strings.Split() function.

Using a positive limit allows you to control the maximum number of substrings returned.

For example:

fmt.Println(strings.SplitN("Hello,World,Welcome", ",", 2))

In this case, strings.SplitN() will split the string “Hello,World,Welcome” at the first occurrence of the delimiter “,” and return two substrings: [“Hello”, “World,Welcome”].

Alternatively, you can use the strings.Split() function without specifying a limit. This will split the string into all substrings based on the delimiter.

For example:

fmt.Println(strings.Split("Hello,World,Welcome", ","))

This will split the string “Hello,World,Welcome” at every occurrence of the delimiter “,” and return all substrings: [“Hello”, “World”, “Welcome”].

By avoiding the use of negative limits with strings.SplitN(), you can write clearer and more predictable code.

Non-Compliant Code Examples

package check

import (
    "strings"
)

func main() {
    strings.SplitN("foo", "o", -1)
    strings.SplitAfterN("foo", "o", -1)
}

Compliant Code Examples

package check

import (
    "strings"
)

func main() {
    strings.SplitN("foo", "o", 10)
    strings.Split("foo", "o")
}
PREVIEWING: aliciascott/DOCS-9725-Cloudcraft