Reputation: 1009
What is the difference between a and b? I know that reflect.DeepEqual considers them not equal and I know that a is nil. Are there built in functions that easily show the difference?
var a []foo
b := []foo{}
Upvotes: 2
Views: 486
Reputation: 1323633
The zero value for a
makes it nil.
nil
for pointers, functions, interfaces, slices, channels, and maps.
As opposed to b
, which is initialized as a short declaration.
Upvotes: 2
Reputation: 36189
fmt.Println(a == nil, b == nil)
prints true false
(Playground: http://play.golang.org/p/E0nQP8dVyE). a
is a nil slice, while b is just an empty slice. There isn't a lot of difference in practice, but usually, say in a function that queries a database, a nil slice means no result (due to error or something else), while an empty slice - that it could not find the information.
For the difference on the lower level, see Russ Cox's Go Data Structures article.
Upvotes: 3