Question
I'm curious as why Go doesn't provide a []byte(*string)
method. From a
performance perspective, wouldn't []byte(string)
make a copy of the input
argument and add more cost (though this seems odd since strings are immutable,
why copy them)?
Answer
[]byte("something")
is not a function (or method) call, it's a type
conversion.
The type conversion "itself" does not copy the value. Converting a string
to
a []byte
however does, and it needs to, because the result byte slice is
mutable , and if a copy would not be made, you could modify / alter the
string
value (the content of the string
) which is immutable , it must be
as the Spec: String types section
dictates:
Strings are immutable: once created, it is impossible to change the contents of a string.
Note that there are few cases when string
<=> []byte
conversion does not
make a copy as it is optimized "away" by the compiler. These are rare and
"hard coded" cases when there is proof an immutable string
cannot / will not
end up modified.
Such an example is looking up a value from a map where the key type is
string
, and you index the map with a []byte
, converted to string
of
course (source):
key := []byte("some key")
var m map[string]T
// ...
v, ok := m[string(key)] // Copying key here is optimized away
Another optimization is when ranging over the bytes of a string
that is
explicitly converted to a byte slice:
s := "something"
for i, v := range []byte(s) { // Copying s is optimized away
// ...
}
(Note that without the conversion the for range
would iterate over the
rune
s of the string and not over its UTF8-encoded bytes.)