Declare slice or make slice?

前端 未结 4 469
情书的邮戳
情书的邮戳 2020-12-12 13:14

In Go, what is the difference between var s []int and s := make([]int, 0)?

I find that both works, but which one is better?

4条回答
  •  暖寄归人
    2020-12-12 13:19

    Just found a difference. If you use

    var list []MyObjects
    

    and then you encode the output as JSON, you get null.

    list := make([]MyObjects, 0)
    

    results in [] as expected.

提交回复
热议问题