'[]byte(string) vs []byte(*string)
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)?
Solution 1:[1]
[]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.)
Solution 2:[2]
I'm curious as why Golang doesn't provide a []byte(*string) method.
Because it doesn't make sense.
A pointer (to any type) cannot be represented (in any obviously meaningful way) as a []byte
.
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)?
Converting from []byte
to string
(and vice versa) does involve a copy, because strings are immutable, but byte arrays are not.
However, using a pointer wouldn't solve that problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Flimzy |