[]byte(string) vs []byte(*string)

huangapple go评论72阅读模式
英文:

[]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(&quot;something&quot;) 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(&quot;some key&quot;)

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 := &quot;something&quot;
for i, v := range []byte(s) { // Copying s is optimized away
    // ...
}

(Note that without the conversion the for range would iterate over the runes 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.

huangapple
  • 本文由 发表于 2017年4月18日 18:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/43470284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定