go - Best practices constructing an empty array in GoLang? -


i'm wondering best practices when initializing empty arrays.

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

myarr1 := []int{} myarr2 := make([]int,0) var myarr3 []int 

i know make empty []int wonder, 1 syntax preferable others? find first readable that's beside point here. 1 key point of contention may array capacity, presumably default capacity same between 3 unspecified. declaring arrays of unspecified capacity "bad"? can assume comes performance cost how "bad" really?

/tldr

  1. is there difference between 3 ways make empty array?
  2. what default capacity of array when unspecified?
  3. what performance cost of using arrays unspecified capacity?

first, it's slice not array. arrays , slices in go different, arrays have fixed size part of type. had trouble @ first :)

  1. not really. if 3 correct, , difference should small worry about. in own code use whatever easiest in particular case.
  2. 0
  3. nothing, until need add item, whatever costs allocate storage needed.

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -