Welcome To Golang By Example

Print string with double quotes in Go (Golang)

Overview

The backslash is the escaping character. To print any string that contains literal double quotes, we need to escape both these quotes when the string is enclosed within double quotes. However, a string enclosed in backquotes is a raw literal string and doesn’t honor any kind of escaping. So back quotes can also be used to print a string with literal double quotes

Program for double-quoted string

package main

import "fmt"

func main() {
	fmt.Println("\"test\"")
}

Output

"test"

Noticed that we escaped both single quotes and double quotes within that string 

Program for back quotes

package main
import "fmt"
func main() {
    fmt.Println(`"test"`)
}

Output

"test"