Use strings.Contains instead of strings.Index with -1
Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel,
n'hésitez pas à nous contacter.
ID: go-best-practices/strings-index-contains
Language: Go
Severity: Notice
Category: Best Practices
Description
In Go, when checking if one string contains another string, it is recommended to use strings.Contains(x, y)
instead of strings.Index(x, y) != -1
.
Here are the reasons why strings.Contains(x, y)
is preferred:
- Simplicity and Readability: Using
strings.Contains(x, y)
provides a more readable and expressive way to check if a string x
contains another string y
. It clearly conveys the intention of the condition without needing an additional comparison operator. - Performance:
strings.Contains(x, y)
is optimized for efficiency and performance. It uses an optimized implementation to check for substring presence, avoiding the need to search for the index and perform an additional comparison. - Avoiding Errors: When using
strings.Index(x, y)
, you need to compare against -1
to check for absence, which can be error-prone. Mistakenly using 0
instead of -1
would lead to incorrect results. Using strings.Contains(x, y)
eliminates the possibility of such mistakes.
For example, consider the following code snippets:
1
2
3
if strings.Contains(x, y) {
// Code block
}
1
2
3
if strings.Index(x, y) != -1 {
// Code block
}
Both snippets check if the string x
contains the substring y
. However, the first snippet using strings.Contains(x, y)
is preferred for its simplicity, readability, and performance benefits.
By using strings.Contains(x, y)
instead of strings.Index(x, y) != -1
, you can write cleaner and more efficient code that adheres to Go’s idiomatic style.
Non-Compliant Code Examples
import (
"fmt"
str2 "strings"
)
func main () {
if str2.Index(x, y) != -1 {
//
}
}
import (
"fmt"
"strings"
)
func main () {
if strings.Index(x, y) != -1 {
//
}
}
Compliant Code Examples
import (
"fmt"
"strings"
)
func main () {
if strings.Contains(x, y) {
//
}
}