Where go is more easily confused

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.