zoukankan      html  css  js  c++  java
  • Golang的单引号、双引号与反引号

    Go语言的字符串类型string在本质上就与其他语言的字符串类型不同:

    • Java的String、C++的std::string以及Python3的str类型都只是定宽字符序列

    • Go语言的字符串是一个用UTF-8编码的变宽字符序列,它的每一个字符都用一个或多个字节表示

    即:一个Go语言字符串是一个任意字节的常量序列

    Golang的双引号和反引号都可用于表示一个常量字符串,不同在于:

    • 双引号用来创建可解析的字符串字面量(支持转义,但不能用来引用多行)

    • 反引号用来创建原生的字符串字面量,这些字符串可能由多行组成(不支持任何转义序列),原生的字符串字面量多用于书写多行消息、HTML以及正则表达式

    而单引号则用于表示Golang的一个特殊类型:rune,类似其他语言的byte但又不完全一样,是指:码点字面量(Unicode code point),不做任何转义的原始内容。

    There are two forms: raw string literals and interpreted string literals.

    • Raw string literals are character sequences between back quotes, as in foo .
    • Interpreted string literals are character sequences between double quotes, as in “bar”.

    A rune literal represents a rune constant, an integer value identifying a Unicode code point. A rune literal is expressed as one or more characters enclosed in single quotes, as in ‘x’ or ‘ ’. Within the quotes, any character may appear except newline and unescaped single quote. A single quoted character represents the Unicode value of the character itself, while multi-character sequences beginning with a backslash encode values in various formats.

    =

    根据我找到的资料以及碰到的情况来看, Go语言的单引号一般用来表示「rune literal」 ,即——码点字面量。

    参考链接:
    • https://golang.org/ref/spec#String_literals
    • https://golang.org/ref/spec#Rune_literals
    • http://teapottable.com/blog/starting-out-with-go-lang/
  • 相关阅读:
    python 自动化之路 day 10 协程、异步IO、队列、缓存
    MySQ binlog三种模式
    文件存储之-内存文件系统tmpfs
    Linux 系统结构详解
    服务端高性能数据库优化演变细节案例
    滴滴研发笔记题,亮灯问题
    linux screen 命令详解
    Linux之在CentOS上一次艰难的木马查杀过程
    python 自动化之路 day 09 进程、线程、协程篇
    redis
  • 原文地址:https://www.cnblogs.com/sunsky303/p/9360336.html
Copyright © 2011-2022 走看看