- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/merge-declaration-assignment
Language: Go
Severity: Info
Category: Best Practices
In Go, it is recommended to avoid using separate variable declaration and assignment statements and instead prefer initializing variables directly in the declaration.
Here are a few reasons why:
var x uint = 1
provides a clear and concise way to express the intent of assigning an initial value to the variable. It reduces unnecessary lines of code, making the code more readable and easier to understand.var x uint
and then assign it a value of 1 separately, it might initially have an undesired default value (in this case, 0) before you assign the actual desired value. By initializing the variable directly in the declaration, you ensure it starts with the desired initial value.Therefore, it is preferable to use var x uint = 1
rather than declaring the variable on one line and assigning it on another. This approach improves code clarity, reduces unnecessary lines, and ensures variables start with the desired initial value.
func main() {
var commands []domain.Command
commands = append(commands, domain.ChangeAttributes{
Command: domain.NewCommand(domain.NewKeyIdentifier(testOrgID, c.CaseID), model.NewUserAuthor(testUserUUID)),
Attributes: domain.CreateAttributesFromProto(
domain.MapArrayToMapListValue(map[string][]string{
"service": {"case-api-test"},
},
)),
})
}
func main () {
var x uint
x = 1
}
func main () {
var generatedUuid uuid.UUID
// not triggering is we have two elements on the left side
generatedUuid, err = uuid.NewUUID()
}
func main () {
var x uint
x = 1
x = 2
}
func main () {
var (
x uint
y int
)
x = 1
x = 2
}
func main () {
var x uint = 1
}