Golang single quotes, double quotes, and anti-quotes

Source: Internet
Author: User

The string type of the go language is string inherently different from the string types in other languages:

    • Java string, C + + std::string, and Python3 str type are all just fixed-width character sequences

    • The go language string is a UTF-8-encoded, variable-width sequence of characters, each of which is represented by one or more bytes

That is: A Go language string is a constant sequence of arbitrary bytes .

Golang's double and back quotes can be used to represent a constant string, except that:

    • Double quotation marks are used to create resolvable string literals (escaped, but cannot be used to refer to multiple lines)

    • Anti-quotes are used to create native string literals, which may consist of multiple lines (no escape sequences are supported), and native string literals are used to write multiple lines of messages, HTML, and regular expressions

The single quotation marks are used to denote a special type of Golang: rune , like other languages, byte but not exactly, refers to code point literals (Unicode code points), without any escaping of the original content.

There is forms:raw string literals and interpreted string literals.

  • Raw string literals is character sequences between back quotes, as in foo  .
  • Interpreted string literals is 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 could 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 the values in various formats.

=

According to the information I found and the situation I encountered, the single quotes of the go language are generally used to denote "rune literal" , the code point literal.

Reference Links:

    • https://golang.org/ref/spec#String_literals
    • https://golang.org/ref/spec#Rune_literals
    • http://teapottable.com/blog/starting-out-with-go-lang/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.