=Start=
搜索關鍵字:
golang single quotes
golang double quotes
golang back quotes
參考結果:
結論寫在最前:在Go語言中不傾向於使用單引號來表示字符串,請根據需要使用雙引號或反引號。
一個Go語言字符串是一個任意字節的常量序列。Go語言的字符串類型在本質上就與其他語言的字符串類型不同。Java的String、C++的std::string以及python3的str類型都只是定寬字符序列,而 Go語言的字符串是一個用UTF-8編碼的變寬字符序列,它的每一個字符都用一個或多個字節表示 。
Go語言中的字符串字面量使用 雙引號 或 反引號 來創建 :
- 雙引號用來創建 可解析的字符串字面量 (支持轉義,但不能用來引用多行);
- 反引號用來創建 原生的字符串字面量 ,這些字符串可能由多行組成(不支持任何轉義序列),原生的字符串字面量多用於書寫多行消息、HTML以及正則表達式。
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/
=EOF=