なし var _ = nil // コンパイルエラー : use of untyped nil in variable declaration → nilは文脈から型が推論できないと使えない。 → nil リテラルそのものは型を持たず、これを untyped nil と呼ぶ。 具象型が確定したnil値(例: var p *MyError の p や (*MyError)(nil))を typed nil と呼ぶ。
for a pointer, channel, func, interface, map, or slice type. ref: https://pkg.go.dev/builtin#nil これらに共通するのは「内部表現にポインタを含む型」であること。 structやarrayにnilは使えない。 The Go gopher was designed by Renée French.
は使える var s []int fmt.Println(s == nil) // true // slice同士の比較はできない( nil同士でも) fmt.Println(([]int)(nil) == ([]int)(nil)) // コンパイルエラー : slice can only be compared to nil → comparableではない型でも == nil だけは仕様で例外的に許可されている。
and T are both unset, (T=nil, V is not set). [...] If we store a nil pointer of type *int inside an interface value, the inner type will be *int: (T=*int, V=nil). Such an interface value will therefore be non-nil even when the pointer value V inside is nil. ref: https://go.dev/doc/faq#nil_error → 公式FAQに`Why is my nil error value not equal to nil?`として、載っているというこ と自体が、多くの人がハマる問題だということを示している。
} return nil // 明示的にnilを返す } To return a proper nil error to the caller, the function must return an explicit nil[...] It's a good idea for functions that return errors always to use the error type in their signature (as we did above) rather than a concrete type such as *MyError, to help guarantee the error is created correctly. ref: https://go.dev/doc/faq#nil_error 戻り値の型が error なら、nilを返すときは明示的に return nil と書く。
!= nil(interface比較)は変えられないから、ポインタ側を変えようという発想。 →`err != nil`を見たとき、「これはnullじゃなくてnilだから、interfaceそのものが空かどう かの判定だ」という語彙レベルの区別。 To be clear, I doubt this proposal will be adopted. But I wanted to write it down as an idea to point to. 本人が「たぶん採用されないかも」と書いている。 ref: https://github.com/golang/go/issues/61489
T で宣言する方法もある)。 *new(T) is, to put it mildly, an embarrassingly clunky way to write the zero value. zero という汎用ゼロ値識別子を追加して、return zero, err と書けるようにする案。 支持は多かったが(p == zeroと*p == zero両方書けてしまう問題などもあり)結局不採 用。 ref: https://github.com/golang/go/issues/61372
will stop pursuing syntactic language changes for error handling. We will also close, without further investigation, all open and new proposals that are limited to syntactic changes to error handling. どの提案もGoチーム内部を含めてコンセンサスに達しなかった。 if err != nil は残り続ける。 → エラーハンドリングの構文すら変えられないなら、nilの意味論を変えるコストはそれ以 上。。 ref: https://go.dev/blog/error-syntax
talks about 19 years on the Go team (Cup o' Go, Episode 111)にて、 The interface can hold any value. It can hold any pointer. It can hold a nil pointer. And that's — you know, we didn't find that confusing, but it's clear that many people do. I wish we had found a different way forward for that even if I don't know what that way is. そして、 At this point in the language, there's probably no answer. → interface は nil ポインタも含めて何でも保持できる。Go チームは当初それを混乱とは思わなかった が、多くの人が混乱しているのは事実。別の設計にできればよかったが、どうすればよかったかはわか らない。そして今の言語の段階では、おそらく解決策はない、という認識。 ref: https://share.transistor.fm/s/9cae9b8d The Go gopher was designed by Renée French.
FAQ: Why is my nil error value not equal to nil? https://go.dev/doc/faq#nil_error • Russ Cox「Go Data Structures: Interfaces」(2009) https://research.swtch.com/interfaces • runtime/runtime2.go https://go.dev/src/runtime/runtime2.go • internal/abi/iface.go https://go.dev/src/internal/abi/iface.go • Francesc Campoy「Understanding Nil」GopherCon 2016 https://speakerdeck.com/campoy/understanding-nil • Ian Lance Taylor on Cup o' Go Episode 111 https://share.transistor.fm/s/9cae9b8d • Dave Cheney「Channel Axioms」 https://dave.cheney.net/2014/03/19/channel-axioms • Go Wiki: Code Review Comments https://go.dev/wiki/CodeReviewComments • Go 101: nils in Go https://go101.org/article/nil.html