- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
",t};e.buildCustomizationMenuUi=t;function n(e){let t='
",t}function s(e){let n=e.filter.currentValue||e.filter.defaultValue,t='${e.filter.label}
`,e.filter.options.forEach(s=>{let o=s.id===n;t+=``}),t+="${e.filter.label}
`,t+=`ID: go-best-practices/modify-parameter
Language: Go
Severity: Info
Category: Best Practices
Assigning new values to function parameters exhibits several bad coding practices and should be avoided for several reasons:
:=
to define a new variable within the function body. This shadows the original parameter, making it inaccessible within the function. Shadowing variables can cause confusion and make the code harder to reason about. It is better to use distinct variable names to maintain clarity and avoid any unintended side effects.To write more maintainable and understandable code, it is advisable to adhere to the following practices:
By following these best practices, the code becomes more readable and easier to manage and avoids introducing unnecessary complexity and confusion.
func fun1(param int) {
// Shadowing 'param'
param := 51
}
func (r *Type) fun2(param int) {
param := 51
}
func fun3(param int) {
if true {
// Outside of the main function body, := always shadows
param, err := doSomething()
}
}
func fun1(param int) {
// Assign a new value to the parameter
param = doSomething()
}
func fun2(param int) {
// In the main function body, := with multiple variables in the left
// will reassign instead of shadowing
param, err := doSomethingElse()
}
func fun3(param int) {
const fun4 = func() {
// Shadowing inside an inner function doesn't affect the outer function
param := doSomething()
}
}
func fun(_ int) {
for true {
// We don't check _ for shadowing
_, err := doSomething()
}
}
func (r *Type) fun1(ctx context.Context) {
// As a special case, we allow shadowing context parameters
ctx := context.WithCancel(ctx)
}
func fun2(ctx context.Context) {
ctx := context.WithCancel(ctx)
}