字符串字面值和字符串值之间的区别是什么?

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

The difference between string literals and string values?

问题

strings博客文章中:

> 有些人认为Go的字符串总是UTF-8,但事实并非如此:只有字符串字面值是UTF-8的。正如我们在前一节中所示,字符串值可以包含任意字节;正如我们在本节中所示,只要没有字节级转义,字符串字面值始终包含UTF-8文本。
>
> 总结一下,字符串可以包含任意字节,但是当从字符串字面值构造时,这些字节(几乎总是)是UTF-8的。

  1. 你能给我一个不是UTF-8的字符串字面值的例子吗?
  2. 在Go中,"string literal"、**"string value""string literal without byte-level escapes"**有什么区别?
英文:

From strings blog post :

> Some people think Go strings are always UTF-8, but they are not: only
> string literals are UTF-8. As we showed in the previous section,
> string values can contain arbitrary bytes; as we showed in this one,
> string literals always contain UTF-8 text as long as they have no
> byte-level escapes.
>
> To summarize, strings can contain arbitrary bytes, but when
> constructed from string literals, those bytes are (almost always)
> UTF-8.

  1. Can you give me an example of a string literal that isn’t an [tag:utf-8] ?
  2. What is the difference in [tag:go] between "string literal" , "string value" , "string literal without byte-level escapes" .

答案1

得分: 7

希望这可以帮到你:

  1. 正如32bitkid所提到的:在Go源代码中,以下字符是一个字符串字面量,其值不是UTF-8编码的:"\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98"

  2. “字符串字面量”的概念仅存在于Go源代码中,没有在编译或运行的程序中表示。在Go源代码中,字符串字面量的写法是"cat dog",如果你的字符串字面量需要包含键盘上没有的内容(或者你的编辑器无法显示),你可以使用“字节级转义”,例如"cat\x07dog"。一旦你的Go源代码被编译,字符串字面量的概念就消失了:只剩下字符串,它们有一些值。这个值可以在代码运行时计算,也可以由源代码中的“字符串字面量”生成的值组成。

“字符串字面量”对于字符串来说就像“数字字面量”对于整数来说:"abc"是一个字符串字面量,而20是一个整数字面量。它们都可以有不同的表示方式,例如"\x61bc"0x14。但是一旦你的代码被编译,你的整数值是来自字面量20还是0x14就没有区别了。字符串也是一样。唯一的复杂之处在于,Go源代码始终是UTF-8编码的。

英文:

Hope this helps:

  1. As 32bitkid mentioned: The following character in Go source code is a string literal whose value is not UTF-8 encoded: "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98".

  2. The idea of a "string literal" exists in Go source code only and has no representation in a compiled or even running program. A string literal in Go source code is written as "cat dog" and if your string literal needs to contain something your keyboard is missing (or your editor cannot display) you may use "byte level escapes" like this "cat\x07dog". Once your Go source code is compiled the notion of a string literal vanishes: There are only strings and they have some value. This value may be computed during the running time of your code or consist of values generated from "string literals" in your source.

"String literals" are to strings what "number literals" ar to ints: "abc" is a string literal and 20 is an int literal. Both may have different representations, e.g. "\x61bc" and 0x14. But once your code is compiled there is no difference whether your int value came from the literal 20 or 0x14. Same with strings. Only complication: Go source code is UTF-8 allways.

huangapple
  • 本文由 发表于 2013年11月30日 00:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/20289574.html
匿名

发表评论

匿名网友

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

确定