What is the difference between backticks (``) & double quotes (“”) in golang?

前端 未结 4 1392
我寻月下人不归
我寻月下人不归 2021-02-01 00:16

What is the difference between backticks (``) & double quotes (\"\") in golang?

相关标签:
4条回答
  • 2021-02-01 00:56

    Backtick strings are analogs of multiline raw string in Python or Scala: r""" text """ or in JavaScript:

    String.raw`Hi\u000A!`
    

    They can:

    1. Span multiple lines.

    2. Ignore special characters.

    They are useful:

    1. For putting big text inside.

    2. For regular expressions when you have lots of backslashes.

    3. For struct tags to put double quotes in.

    0 讨论(0)
  • 2021-02-01 01:01

    In quotes "" you need to escape new lines, tabs and other characters that do not need to be escaped in backticks ``. If you put a line break in a backtick string, it is interpreted as a '\n' character, see https://golang.org/ref/spec#String_literals

    Thus, if you say \n in a backtick string, it will be interpreted as the literal backslash and character n.

    0 讨论(0)
  • 2021-02-01 01:03

    `` represents an uninterpreted strings and "" is an interpreted string.

    The value of a raw string literal (uninterpreted strings) is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes

    Interpreted string literals are character sequences between double quotes, as in "bar". Within the quotes, any character may appear except newline and unescaped double quote.

    PS: italicized words are mine

    https://golang.org/ref/spec#String_literals

    0 讨论(0)
  • 2021-02-01 01:04

    Raw string literals are character sequences between backticks. Backslashs ('\') have no special meaning and Carriage return characters ('\r') inside raw string literals are discarded from the raw string value.

    Interpreted string literals are character sequences between double quotes ("\r", "\n", ...)

    source: http://ispycode.com/GO/Strings/Raw-string-literals

    0 讨论(0)
提交回复
热议问题