2017-07-26 03:46
浏览 4.1k


I'm wondering about best practices when initializing empty arrays.

i.e. Is there any difference here between arr1, arr2, and arr3?

myArr1 := []int{}
myArr2 := make([]int,0)
var myArr3 []int

I know that they make empty []int but I wonder, is one syntax preferable to the others? Personally I find the first to be most readable but that's beside the point here. One key point of contention may be the array capacity, presumably the default capacity is the same between the three as it is unspecified. Is declaring arrays of unspecified capacity "bad"? I can assume it comes with some performance cost but how "bad" is it really?


  1. Is there any difference between the 3 ways to make an empty array?
  2. What is the default capacity of an array when unspecified?
  3. What is the performance cost of using arrays with unspecified capacity?

图片转代码服务由CSDN问答提供 功能建议

我想知道初始化空数组时的最佳实践。</ p>

\ n

ie 这里的arr1,arr2和arr3有什么区别吗?</ p> </ blockquote>

  myArr1:= [] int {} 
myArr2:= make([] int  ,0)
var myArr3 [] int 
 </ code> </ pre> 

我知道他们将空的 [] int </ code>设为空,但是我想知道,哪种语法更可取 给其他人? 就我个人而言,我发现第一个最易读,但这与本文无关。 争用的一个关键点可能是阵列容量,这三个之间的默认容量可能是相同的,因为未指定。 声明未指定容量的数组是否“不好”? 我可以假设它会带来一些性能成本,但是它到底有多“糟糕”?</ p>

/ tldr </ p> </ blockquote>

  1. 制作空 数组的三种方法之间是否有区别? </ li>
  2. 未指定时阵列的默认容量是多少?</ li>
  3. 使用未指定容量的阵列的性能成本是多少?</ li> </ ol > </ div>

3条回答 默认 最新

相关推荐 更多相似问题