Go project migrated from 1.1 to 1.3 error occurred

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently, engaged in a Go language project, the current Google to upgrade the go language to 1.3 version, the results have some problems, now share the following:

The code in the project is to use sprintf to detect whether two objects are the same,

Datas: = Make (Map[string][]byte) datas["Jonny"] = []byte ("Hsu") if FMT. Sprintf ("% #v", datas["jonn") = = Fmt. Sprintf ("% #v", []byte{}) {    fmt. Println ("------------   Equal---------------")} else {    fmt. Println ("============not equal===============")}


However, it is strange that in the go1.1 version of the successful output of equal but to go1.3 under the lack of output is not equal information.

The reason is that one output []byte (nil) and the other one outputs []byte{}, so the resulting inequality

By careful analysis and comparison, the use of the Command Grep-rn "byte (nil)" In the SRC folder of go found in the FMT print file in go1.3:


Func (P *pp) fmtbytes (v []byte, verb rune, gosyntax bool, Typ reflect. Type, Depth int) {if verb = = ' V ' | | verb = = ' d ' {if gosyntax {if v = = Nil { if Typ = = Nil { p.buf.writestring ("[]byte (Nil)")                               } else {&N Bsp                          ,         &NB Sp   P.buf.writestring (Typ. String ())                                 & nbsp       P.buf.write (nilparenbytes)                     &NBSP ;          }                        &N Bsp       return                       }  &NB Sp                     If Typ = nil {          &N Bsp                     P.buf.write (byteSbytes                        } else {      &N Bsp                         p.buf.writestring (Typ) String ())                                 p . buf. WriteByte (' {')                        }      &N Bsp        } else {                        p. Buf. WriteByte (' [')                }              &N Bsp For I, c: = Range v {                        If i > 0 {&nbs P                               if Gosyntax {&nbs P   &NBSp                          ,         P.B uf. Write (commaspacebytes)                             &N Bsp   Else {                              &N Bsp         p.buf.writebyte (')                     &NBS P          }                       }& nbsp                       P.printarg (c, ' V ', P.fmt.plus, Gosyntax, Dept h+1)                }


It can be known that []byte (nil) is output here, in the same function, the source code in go1.1 is:


Func (P *pp) fmtbytes (v []byte, verb rune, gosyntax bool, Typ reflect. Type, Depth int) {if verb = = ' V ' | | verb = = ' d ' {if Gosyntax { if Typ = = Nil { p.buf.write (bytesbytes)                         } else {                              &NBSP ; P.buf.writestring (Typ. String ())                                 p . buf. WriteByte (' {')                        }      &N Bsp        } else {                        p. Buf. WriteByte (' [')                }              &N Bsp For I, c: = Range v {                        If i > 0 {&nbs P                               if Gosyntax {&nbs P                                       P.buf.write (commaspacebytes)                 &N Bsp              } else {                  &N Bsp                     p.buf.writebyte (')         &NBS P                      }            &N Bsp          }
After comparison, it can be understood that in go1.3, the go language is more clear about the []byte array and the empty []byte type] that have no elements.

Comprehensive comprehension:

go1.1 processing mode and go1.3 different treatment, out of the problem can find the source of go, dig deep, can find relevant clues.

Share here to feast on the Yimeimei.


by JONNYHSU

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.