A string is immutable, a byte slice is not. Suppose you have:
b := []byte("hello world") s := string(b) fmt.Println(s) b[0] = 'H' fmt.Println(s) What is printed? hello world hello word Using the OPs function: b := []byte("hello world") s := fastBytesToString(b) fmt.Println(s) b[0] = 'H' fmt.Println(s) This results in hello world Hello world So the immutable string pointed to by s has been mutated, so this program violates Go's memory model. "casting" a byte slice to/from a string involves a memory allocation and copy, to be valid Go. -Paul On Mon, Sep 19, 2016 at 3:33 PM, 'simon place' via golang-nuts < golang-nuts@googlegroups.com> wrote: > i thought strings were just byte[]s, when you 'cast' all you do is tell > the compiler to change the way the underlying bytes can are used. it > doesn't 'cost' anything. > > -- > 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. > -- 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.