- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/simplify-make
Language: Go
Severity: Info
Category: Best Practices
In Go, developers should avoid using make([]int, 0)
and instead use make([]int)
.
Here are a few reasons why:
make([]int)
instead of make([]int, 0)
is simpler and clearer in its intent. The make([]int)
syntax creates a slice with a length and capacity of 0, indicating an empty slice. It avoids unnecessary redundancy by omitting the explicit specification of a capacity of 0.make([]int)
expression is more efficient in terms of memory allocation. When creating a slice with a length and capacity of 0, Go’s underlying implementation already handles the allocation and management of an empty slice efficiently. Explicitly specifying a capacity of 0 in make([]int, 0)
doesn’t provide any additional benefit in terms of performance.make([]int)
is easier to read and maintain because it represents the intent of creating an empty slice explicitly. It aligns with the idiomatic Go style and is widely recognized and understood by the Go community.In summary, it is recommended to use make([]int)
when creating an empty slice in Go instead of make([]int, 0)
. This approach simplifies the code, improves its readability, and ensures efficient memory allocation.
func main () {
foo := make([]int, 0, 0)
foo := make(map[string]string, 0)
}
func main () {
foo := make([]int, 0)
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products