This is a creation in Article, where the information may have evolved or changed.
Where go is more easily confused
Go is easy to confuse in terms of nil judgment.
Nil
Go Nil is band type, you can refer to the relevant article. Look at the following code:
Import("IO" "FMT")typeMywriterstruct{}func(W *mywriter) Write (P []byte) (nint, err Error) {return}funcMain () {varM *mywriter FMT. Println ("ptr m==nil?",BOOL(m==Nil))varo io. Writer = M fmt. Println ("interface O==nil?",BOOL(o==Nil))ifA,ok: = O. (*mywriter);true{FMT. Println ("Dynamic covert OK?"Ok"and A==nil?",BOOL(a==Nil))} o =func() Io. writer{varM *mywriterreturnM} () Fmt. Println ("return o==nil?",BOOL(o==Nil))func(o io. Writer) {fmt. Println ("The params o==nil?",BOOL(o==Nil))} (m) OS. Exit(0)}
The results are quite peculiar:
ptr m==niltrueinterface o==nilfalsedynamictrueand a==niltruereturn o==nilfalseparams o==nilfalse
The simple rule is one, nil is of a type:
varnil// 实际上是带类型的(type, value):var m *MyWriter = ((*MyWriter)nilnil)
Therefore, when comparing, it is the band type comparison:
varnilifnil {// 相当于:if m == ((*MyWriter)nilnil) {// 这个地方当然是相等的,类型等,值也等。
But other types are not equal:
nilio.Writer = m//这时候的o实际上是:((*MyWriter)nil,nil)if o == ((io.Writer)nilnil) {
This is important when returning values and parameters, especially parameters:
func(o io.Writer) { fmt.Println("params o==nil?"bool(o==nil)) }(m)
Most of the time, the pass-through is definitely not the interface, but a pointer, unless directly called, otherwise it is non-nil, only the following direct invocation condition is nil:
func(o io.Writer) { fmt.Println("params o==nil?"bool(o==nil)) }(nil)
This is really easy to confuse.