Go語言的字串類型string
在本質上就與其他語言的字串類型不同:
即:一個Go語言字串是一個任意位元組的常量序列。
Golang的雙引號和反引號都可用於表示一個常量字串,不同在於:
而單引號則用於表示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 ‘\n’. 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/