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