In theory the compiler can be smart enough, if it knows that the string
passed to fmt.Println is not stored and re-used someplace, but I do not
believe that is currently true. It probably has at least 2 copies, one to
the string in your code and a second one to make it a []byte to pass to the
io.
well, seems to me if you need to access a string by index, you are assuming
1 byte per char. say ASCII, so for me you should be using []byte to store
it, then 'cast' to see it as a string.
https://play.golang.org/p/2NMye8gnzg
surely this doesn't do any copying?
--
You received this message be
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.
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