- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/err-prefixed-with-err
Language: Go
Severity: Notice
Category: Best Practices
In Go, it is a recommended convention to prefix error variables with err
. This convention helps improve code readability and maintainability by providing a consistent and recognizable pattern for error handling. Here are the reasons why error variables should be prefixed with err
:
err
, it becomes immediately clear that the variable represents an error value. This makes it easier for developers to identify error-related variables at a glance and understand their purpose within the code.err
prefix enhances code readability by establishing a consistent naming convention for error variables across the codebase. When reading the code, it becomes apparent that a variable is an error by simply looking at its name. This promotes clarity and reduces the cognitive load when reviewing or maintaining the code.err
makes it easier to search for error-related variables in a codebase. Developers can quickly search for variables starting with err
to locate and review error handling logic. This simplifies debugging and troubleshooting efforts when dealing with errors in complex codebases.err
aligns with common error handling patterns in Go. For example, it is customary to check for error values using if err != nil
or if foo, err := bar(); err != nil
constructions. By following this convention, developers can easily identify and apply these error handling patterns consistently throughout the codebase.It’s important to note that this convention is not enforced by the Go language itself and is not a strict requirement. However, it is widely adopted and considered good practice within the Go community. Adhering to this convention provides numerous benefits for code readability, maintainability, and consistency in error handling code.
package main
import ("errors")
func main(){
myErr := errors.New("myErr")
myErr2 := fmt.Errorf("myError2")
myErr3 := fmt.New("myErr3") // Technically a false positive, but this is not a real method.
myErr4 := errors.Errorf("myErr4") // Ditto
fmt.Println(myErr, myError2, myErr3, myErr4)
}
package main
import ("errors")
func main(){
err := errors.New("myErr")
Err2 := fmt.Errorf("myError2")
fmt.Println(err, Err2)
}