I have this text that I would like to add to a byte fragment:
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 it, at runtime it will be:
- create a string with values ββin memory
- create byte fragment
- copy the contents of the string to a byte slice (redistributing if necessary)
I could convert each of the string values ββto their ASCII equivalent and directly create a byte slice:
b := []byte{84, 104, ... }
although it is not very readable.
I understand that the example here is a little trivial, and most computers can do this instantly, but I'm interested. Does the compiler interpret []byte("blah") and turn it into an efficient byte fragment at compile time? Would a better solution change if the string contains non-ASCII characters?
source share