- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/duration-variable-names
Language: Go
Severity: Info
Category: Best Practices
In Go, the time.Duration
type represents a duration of time. It is an integer or float type that represents the length of a duration in units such as seconds, milliseconds, or nanoseconds.
When working with time.Duration
variables in Go, it is important to note that the type itself already represents a duration with a specific unit. The unit of the duration is determined by the context in which it is used. Therefore, it is not necessary to explicitly include the unit in the variable name or identifier.
Here are a few reasons why we should avoid using time units in variable names for Go’s time.Duration
type:
time.Duration
type already implies that the value represents a duration.retryTimeout
instead of retryTimeoutSeconds
, it can be easily changed to represent a different unit (e.g., milliseconds) without affecting the variable name.time
package, follows the convention of using the type name (Duration
) without including time units in variable names. By following this convention, our code aligns better with the standard library and promotes code consistency.To ensure good coding practices, it is recommended to name Go time.Duration
variables in a descriptive and context-specific manner without explicitly mentioning the time unit in the variable name. This approach leads to clear and maintainable code.
func main () {
var foo time.Duration
var xms time.Duration
var xy, xms time.Duration
var blaMs time.Duration
xms := 5 * time.Second
xms := 5 * time.Millisecond
}
func main () {
var fooDuration time.Duration
timeToWait := 5 * time.Second
}