arrays - Where do the square brackets come from? -


package main  import (     "fmt"     "log" )  func main() {     := []string{"abc", "edf"}     log.println(fmt.sprint(a)) } 

the above go program print following output, slice value inside square brackets "[]".

2009/11/10 23:00:00 [abc edf]

and want know in source code [] added formatted string.

i checked source code src/fmt/print.go file, couldn't find exact line of code this.

could provide hint?

you printing value of slice. formatted / printed in print.go, unexported function printreflectvalue(), line #980:

855 func (p *pp) printreflectvalue(value reflect.value, verb rune, depth int)                                            (wasstring bool) {          // ...  947     case reflect.array, reflect.slice:             // ...  979         } else { 980             p.buf.writebyte('[') 981         } 

and line #995:

994         } else { 995             p.buf.writebyte(']') 996         } 

note "general" slices (like []string), byte slices handled differently:

948         // byte slices special: 949         // - handle []byte (== []uint8) fmtbytes. 950         // - handle []t, t named byte type, fmtbytes 

[]byte printed in unexported function fmtbytes():

533 func (p *pp) fmtbytes(v []byte, verb rune, typ reflect.type, depth int) {              // ...  551         } else { 552             p.buf.writebyte('[') 553         }              // ...  566         } else { 567             p.buf.writebyte(']') 568         } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -