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

6

148

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

Ramiform answered 24/10, 2017 at 18:12 Comment(0)
N
142

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.
Nnw answered 24/10, 2017 at 18:14 Comment(1)
So in C# terms backtick `` is like @"" while the double-quote "" is exactly like C# doublequote strings. That's neat. Shame though that the JS world employed backticks `` a few years down the road for string interpolation - something which at the time of this writing is not supported at all in GoLang. I'm saying this simple because JS is prevailing nowadays and it will make a lot of devs scratch their heads when they are picking up GoLang.Recuperator
F
48

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.

Forbis answered 25/10, 2017 at 6:29 Comment(0)
R
11

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

Ramiform answered 24/10, 2017 at 18:36 Comment(0)
A
9

`` 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

Attending answered 20/4, 2019 at 5:51 Comment(0)
U
7

golang has three types of quotations. Single quote , double quote, or backquotes (backticks)

  • Single quote - indicates a byte type or rune type which corresponds to uint8 or int32 as well as the default rune type. Usually used to denote rune types and display Unicode.
  • Double quote - indicate strings which are char arrays. Hence you can use array index to access bytes or use functions like len().
  • Back quotes (backtick) - indicate a string literal, but does not support escape sequences. Usually used to display string literals like multiple lines
Unctuous answered 7/3, 2021 at 14:38 Comment(0)
Z
0

Double Quotes:

message := "Hello, World!"
fmt.Println(message) // Output: Hello, World!

Backticks (`):

message := `Hello, \n World!`
fmt.Println(message) // Output: Hello, \n World!
Zoophilia answered 14/4 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.