- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/avoid-bare-return
Language: Go
Severity: Info
Category: Best Practices
The “Avoid bare returns” rule in Go static analysis is designed to increase clarity and readability in your code. A bare return is when a function that has named return values returns those values implicitly, without explicitly stating what is being returned.
While Go’s allowance for bare returns can make code more concise, it can also make it more difficult to understand and debug, especially in larger functions. Implicitly relying on the state of named return values can lead to unexpected behavior if those values are modified elsewhere in the function.
To adhere to this rule and promote better coding practices, always explicitly return values in your functions. This makes it clear what values are being returned and in what state, reducing the chance of bugs and making your code easier to understand. For example, instead of writing return
in a function that returns an int
and a bool
, write return 0, false
.
func func1(arg int) (a int) {
return
}
func func2(arg int) (b int) {
return
}
func func3(arg int) (a int, b bool) {
return
}
func func4(a string, b int) (a int, b string) {
return
}
func func1(arg int) {
return
}
func func2(arg int) int {
return 4
}
func func3(arg int) (int, bool) {
return 3, false
}
func func3(arg int) (int, bool) {
return 3, true
}
func func2(arg int) int {
return
}
func func3(arg int) (int, bool) {
return
}
func func3(arg int) (int, bool) {
return 3, true
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products