Correct way to initialize empty slice

前端 未结 4 1019
春和景丽
春和景丽 2020-12-04 05:50

To declare an empty slice, with a non-fixed size, is it better to do:

mySlice1 := make([]int, 0)

or:

mySlice2 := []int{}
         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 06:16

    The two alternative you gave are semantically identical, but using make([]int, 0) will result in an internal call to runtime.makeslice (Go 1.14).

    You also have the option to leave it with a nil value:

    var myslice []int
    

    As written in the Golang.org blog:

    a nil slice is functionally equivalent to a zero-length slice, even though it points to nothing. It has length zero and can be appended to, with allocation.

    A nil slice will however json.Marshal() into "null" whereas an empty slice will marshal into "[]", as pointed out by @farwayer.

    None of the above options will cause any allocation, as pointed out by @ArmanOrdookhani.

提交回复
热议问题