rapid icon indicating copy to clipboard operation
rapid copied to clipboard

Make sure we return nil slices as empty slices sometimes

Open flyingmutant opened this issue 4 years ago • 1 comments

As there is code that can work with a := []int{}, but fail with var a []int.

Can we treat empty slices as having something like len = -1 during generation?

flyingmutant avatar Jul 26 '20 17:07 flyingmutant

The only time I can think of where go will handle those differently is in comparison to nil. For example, the following will only print "a is nil" once:

var a []int
if a == nil {
    fmt.Println("a is nil")  // this will print
}
a = []int{}
if a == nil {
    fmt.Println("a is nil")  // this won't
}

wfscheper avatar Nov 01 '20 00:11 wfscheper

Closing as it looks like there is not a lot of value in this.

flyingmutant avatar Aug 22 '22 09:08 flyingmutant