What is the difference between backticks (``) & double quotes ("") in golang?
Solution 1:
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.
a := "\n" // This is one character, a line break.
b := `\n` // These are two characters, backslash followed by letter n.
Solution 2:
Backtick strings are analogs of multiline raw string in Python or Scala: r""" text """
or in JavaScript:
String.raw`Hi\u000A!`
They can:
Span multiple lines.
Ignore special characters.
They are useful:
For putting big text inside.
For regular expressions when you have lots of backslashes.
For struct tags to put double quotes in.
Solution 3:
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