uber-go / guide

The Uber Go Style Guide.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"nil is a valid slice" issue

alexus1024 opened this issue · comments

Actually []int{} and nil are not equivalent. Replacement of one to another may cause broken api.

Example:
https://play.golang.org/p/MojffFnM8Gn

There should be warning about that.

"nil is a valid slice" does not mean []T(nil) == []T{}. Slice operations on these are identical, because nil slices behave the same as non-nil slices, but they are not equivalent - one has been allocated, the other has not. Their serialized formats may or may not be the same, but that's an implementation detail of the JSON encoder more than anything else.

(It's also worth noting that, if the difference between []T(nil) and []T{} breaks expectations or semantics in an API, there may be design/usage issues at hand.)

The guidance is sound as written, but we can certainly add a reminder that []T(nil) != []T{}.

Thank you. This is exact solution I wanted.