這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
=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以及Regex。
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=