0
0

More than 1 year has passed since last update.

nil slice と empty slice

Posted at

sliceを扱う時、結果が入っていない空のsliceなのか、それともsliceはnilなのかを意識せずにテストを書いて、テストでこけたことがあった。

nil slice と empty slice の違いはこんな感じ。

var a, b, c, d, e []int

b = nil
c = []int(nil)
d = []int{}
e = make([]int, 0)

// nil slice   --> a,b,c
// empty slice --> d,e
fmt.Printf("%#v", a) // []int(nil)
fmt.Printf("%#v", b) // []int(nil)
fmt.Printf("%#v", c) // []int(nil)
fmt.Printf("%#v", d) // []int{}
fmt.Printf("%#v", e) // []int{}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0