- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/single-case-select
Language: Go
Severity: Info
Category: Best Practices
In Go, using a select
statement with a single case is generally considered a code smell and not a recommended practice.
Here are a few reasons why:
select
statement is to select and execute the first case
that is ready to proceed. When there is only a single case
in the select
statement, it becomes redundant and obscures the clarity of the code. It is better to use a straightforward control flow statement like if
or switch
instead, as it conveys the intent more directly.select
statement with a single case can make the code harder to read and understand. It adds unnecessary complexity and can confuse other developers who are reviewing or maintaining the codebase. It’s important to write code that is easy to comprehend and follow.select
statement limits the extensibility of the code. If additional cases need to be added in the future, it becomes clumsy to modify the code by converting the single case into a default
case or by adding more cases. It’s better to start with a more flexible structure, such as an if
statement, that can accommodate future additions more easily.case
in a select
statement as a potential code issue or a pattern that can be simplified. This can result in unnecessary noise and make it harder to identify genuine issues when reviewing the codebase with these tools.To improve code readability and maintainability, it is recommended to use select
statements when there are multiple cases with different synchronization or communication requirements. For a single case, it is clearer and more straightforward to use an if
statement or a simple control flow construct.
func main () {
select {
case <-ch: {
}
}
}
func main () {
select {
case <-ch1: {
}
case <-ch2: {
}
}
}