some more uses: string([]byte):
// Some internal compiler optimizations use this function. // - Used for m[string(k)] lookup where m is a string-keyed map and k is a []byte. // - Used for "<"+string(b)+">" concatenation where b is []byte. // - Used for string(b)=="foo" comparison where b is []byte. https://github.com/golang/go/blob/master/src/runtime/string.go#L125 https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/walk.go#L1448 the other way around: []byte(string) // The only such case today is: // for i, c := range []byte(string) https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/walk.go#L1485 Martin On Tuesday, February 14, 2017 at 12:39:40 AM UTC+1, Ian Lance Taylor wrote: > > On Mon, Feb 13, 2017 at 12:31 PM, Alex Flint <alex....@gmail.com > <javascript:>> wrote: > > > > As of go1.8, do conversions between strings and byte slices always > generate > > a copy? > > Usually but not absolutely always. > > The gc compiler has an optimization for map lookups. For a > map[string]T, when s is a []byte, m[string(s)] will not make a copy. > > I'm not aware of any other similar slice <-> string optimization in > the gc compiler. There could be some that I don't know about. > > Ian > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.