英文:
[]byte(string) vs []byte(*string)
问题
我很好奇为什么Go语言没有提供[]byte(*string)
方法。从性能的角度来看,[]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)?
答案1
得分: 26
[]byte("something")
不是一个函数(或方法)调用,而是一个类型转换。
类型转换本身不会复制值。但是,将 string
转换为 []byte
会进行复制,这是必要的,因为结果的字节切片是可变的。如果不进行复制,你可以修改/更改 string
值(string
的内容),而 string
是不可变的,正如 规范:字符串类型 部分所规定的那样:
字符串是不可变的:一旦创建,就无法更改字符串的内容。
请注意,有一些情况下,string
<=> []byte
的转换不会进行复制,因为编译器会对其进行优化。这些情况很少见,并且是在有证据证明不可变的 string
不会被修改的情况下进行的。
一个例子是从一个键类型为 string
的映射中查找值,并使用转换为 string
的 []byte
进行索引(来源):
key := []byte("some key")
var m map[string]T
// ...
v, ok := m[string(key)] // 这里复制 key 被优化掉了
另一个优化是在显式转换为字节切片的字符串的字节上进行迭代:
s := "something"
for i, v := range []byte(s) { // 这里复制 s 被优化掉了
// ...
}
(请注意,如果没有进行转换,for range
将迭代字符串的 rune
而不是其 UTF8 编码的字节。)
英文:
[]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.)
答案2
得分: 5
因为这没有意义。
指针(任何类型的指针)不能以任何明显有意义的方式表示为[]byte
。
从性能的角度来看,[]byte(string)
会复制输入参数并增加更多的开销(尽管这似乎很奇怪,因为字符串是不可变的,为什么要复制它们)。
将[]byte
转换为string
(反之亦然)确实涉及复制,因为字符串是不可变的,但字节数组不是。
然而,使用指针并不能解决这个问题。
英文:
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论