Golang: String Backslash Escape

By Xah Lee. Date: . Last updated: .

Within a double quoted string, character sequence starting with backslash may have special meaning. example: \n means newline.

package main

import "fmt"

func main() {
	var x = "a\nb"
	fmt.Println(x)
}

// prints
// a
// b

Here's complete list:

\a
U+0007 alert or bell
\b
U+0008 backspace
\f
U+000C form feed
\n
U+000A line feed or newline
\r
U+000D carriage return
\t
U+0009 horizontal tab
\v
U+000b vertical tab
\\
U+005c backslash
\"
U+0022 double quote
\ooo
o is octal digit.
\xhh
a byte. h here is a hexadecimal digit.
\uhhhh
a Unicode character whose codepoint can be expressed in 4 hexadecimal digits. (pad 0 in front)
\Uhhhhhhhh
a Unicode character whose codepoint can be expressed in 8 hexadecimal digits. (pad 0 in front)

[see ASCII Table]

package main

import "fmt"

func main() {
	fmt.Printf("%v\n", "A" == "\x41")       // true
	fmt.Printf("%v\n", "♥" == "\u2665")     // true
	fmt.Printf("%v\n", "😂" == "\U0001f602") // true
}

Golang

Examples

Reference