Golang: String Backslash Escape

By Xah Lee. Date: . Last updated: .

What is String Backslash Escape

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

List of String Backslash Escapes

\a
U+0007 alert or bell. [see ASCII Characters]
\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 character whose Unicode Codepoint can be expressed in 4 Hexadecimal digits. (pad 0 in front)
\Uhhhhhhhh
a character whose Unicode Codepoint can be expressed in 8 Hexadecimal digits. (pad 0 in front)
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 String