dongyan7988 2013-11-29 16:03
浏览 77
已采纳

字符串文字和字符串值之间的区别?

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 utf-8 ?
  2. What is the difference in go between "string literal" , "string value" , "string literal without byte-level escapes" .
  • 写回答

1条回答 默认 最新

  • dra11767 2013-11-29 16:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?