This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:go-best-practices/omit-default-slice-index
Language: Go
Severity: Warning
Category: Best Practices
Description
In Go, the expression s[n:len(s)] is used to slice a string or slice s starting from index n up to the end of s. However, it is considered suboptimal and can be replaced with the simpler and more expressive s[n:] notation.
Using s[n:len(s)] is not optimal for a few reasons:
Readability: The s[n:] notation provides a clearer and more concise representation of slicing from index n to the end of s. It eliminates the need to explicitly specify len(s), making the code more readable.
Simplicity: By using s[n:], you remove unnecessary redundancy in the code. It improves the simplicity of your code and reduces the chances of introducing errors when manually specifying the length of s.
Performance: Although the performance difference may be negligible, using s[n:] is more efficient than creating a len(s) expression. The s[n:] notation directly references the underlying slice without requiring an additional length calculation.
For example, let’s consider the following code snippets:
s:="Hello, World!"fmt.Println(s[7:len(s)])
Output: “World!”
s:="Hello, World!"fmt.Println(s[7:])
Output: “World!”
Both snippets will produce the same output, but the second one using s[7:] is preferred for its simplicity and readability.
By replacing s[n:len(s)] with s[n:], you can improve the clarity and maintainability of your code while still achieving the desired slicing functionality.
Non-Compliant Code Examples
funcmain(){d:=s[n:len(s)]}
Compliant Code Examples
funcmain(){d:=s[n:]}
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- go-best-practices # Rules to enforce Go best practices.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다