I have this text which I would like to put into a byte slice:
s := "There are these two young fish swimming along and they happen to meet an older fish swimming the other way"
If I write
b := []byte("There are these two young fish swimming along and they happen to meet an older fish swimming the other way")
As I understand, at runtime this will:
- create a string with the values in memory
- create a byte slice
- copy the contents of the string into the byte slice (reallocating as necessary)
I could convert each of the string values to their ASCII equivalent and create the byte slice directly:
b := []byte{84, 104, ... }
though this isn't very readable.
I understand that the example here is a little trivial and most computers can do this in a flash, but I'm curious about it. Does the compiler interpret []byte("blah")
and turn it into an efficient byte slice at compile time? Would the best solution change if the string included non-ASCII characters?