- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/for-select-default-empty
Language: Go
Severity: Notice
Category: Best Practices
In Go, the default case of a switch statement is executed when none of the preceding cases match the specified condition.
While it is technically allowed to have an empty default case, it is generally considered a bad practice because it can lead to confusion and make the code harder to read and maintain.
Here are a few reasons why the default case should not be empty in Go:
To address these concerns, it is recommended to either provide a meaningful action or simply include a comment in the default case explaining the reason for its presence. This helps improve code clarity, maintainability, and ensures proper handling of all possible cases in the switch statement.
func main () {
for {
select {
case <-myChannel:
default:
}
}
}
func main () {
for {
select {
case <-myChannel:
println("foo")
default:
println("bar")
}
}
for {
select {
case <-myChannel2:
println("foo")
default:
// println("bar")
}
}
for something {
select {
case <-myChannel2:
println("foo")
default:
}
}
select {
case <-myChannel:
default:
}
for {
select {
case <-myChannel:
default:
}
println("foo") // another statement after for, no warning
}
}