Comment on A new experimental Go API for JSONparentComments−stackedinserter1yWhy shouldn't it be? The nil is null and empty array is an empty array, they are completely different objects.−maleldil1yNot in Go. Empty slices and empty maps are nil, so it's ambiguous.−rowanseymour1yTo be precise.. empty slices and maps sometimes behave like nil (len, range etc) and sometimes not (inserting into a nil map). The former is a neat convenience, and I think extending that to JSON marshaling makes sense.−stackedinserter1yNo, empty slices and empty maps in Go are not nil.−maleldil1yThis is the idiomatic way of declaring empty slices in Go, and it prints true: var slice []string fmt.Println(slice == nil)−everybodyknows1yWhether to judge the line below idiomatic, or not, is a question I leave to the authorities -- but it is highly convenient, and prints "false". slice := []string{} fmt.Println(slice == nil)−tux31yThis is indeed a nil slice, and it does have len() == 0, but Go also has a concept of empty slices separate from nil slicesThe language just has a bad habit of confusing them some of the time, but not consistently, so you can still occasionally get bit by the differenceAs someone who uses Go a lot, it's just one of those things...−stackedinserter1yYes because it's nil. You declared it but not created. Same for map. Same for var something *string
Comments
Why shouldn't it be? The nil is null and empty array is an empty array, they are completely different objects.
Not in Go. Empty slices and empty maps are nil, so it's ambiguous.
To be precise.. empty slices and maps sometimes behave like nil (len, range etc) and sometimes not (inserting into a nil map). The former is a neat convenience, and I think extending that to JSON marshaling makes sense.
No, empty slices and empty maps in Go are not nil.
This is the idiomatic way of declaring empty slices in Go, and it prints true:
Whether to judge the line below idiomatic, or not, is a question I leave to the authorities -- but it is highly convenient, and prints "false".
This is indeed a nil slice, and it does have len() == 0, but Go also has a concept of empty slices separate from nil slices
The language just has a bad habit of confusing them some of the time, but not consistently, so you can still occasionally get bit by the difference
As someone who uses Go a lot, it's just one of those things...
Yes because it's nil. You declared it but not created. Same for map. Same for var something *string