Welcome To Golang By Example

Write to write or print backslash in a string in Go (Golang)

Overview

Backlash is an escaping character. To print a backslash we need to first escape is with another backslash character when using double-quotes. However, a backslash can also be printed using backquotes. It is also used to define a string. A string encoded in backquotes is a raw literal string and doesn’t honor any kind of escaping.

Program for double quotes

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

Output

\test

A string defined within double quotes will honour escaping characters. That is why we need to escape backlash. Let’s see a program for back quotes

Program for back quotes

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

Output

\test

In this case, we don’t need any escaping as with back quotes a string is a raw literal string