Golang escape backslash and escape string: raw literals vs "quoted"

Golang escape string and go escape string: golang escape backslash with doubled backslash in double-quoted strings, golang escape characters in interpreted literals, raw string literals for paths, and golang escape backtick with rune or hex escape; links to string literals in the spec.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang escape backslash and escape string: raw literals vs "quoted"

People searching for golang escape, golang escape string, go escape string, or golang escape backslash are usually writing interpreted string literals in double quotes, where \ starts an escape sequence. A golang escape string problem often comes down to doubling \ or switching to a raw string literal between backticks. Golang escape characters follow the Go string literal rules. For multiline and raw-string style, see multiline strings in Go. Queries about Java on this URL are a different language; this page is Go-only.

Tested with Go 1.24 on Linux.


Double-quoted strings: double the backslash

Inside "...", a single \ must introduce a known escape or another \. To print one backslash, use \\:

go
package main

import "fmt"

func main() {
	fmt.Println(`\Welcome to GoLinuxCloud\`)
	fmt.Println("\\Welcome to GoLinuxCloud\\")
	fmt.Println("Which is better: Golang \\\\ Java?")
}
Output

You should see the same path-style line from the raw string and the interpreted string, then Which is better: Golang \\ Java? with two visible backslashes before Java.

If a \ is not followed by a valid continuation and is not the second half of \\, the literal will not compile—especially a trailing \ right before the closing ".


Raw string literals (fewer escapes)

Between backticks `...`, backslashes are usually literal, which is why go escape string style paths like `C:\Users\name` are readable. You still cannot put a bare backtick inside a raw string without ending it; split the string or use concatenation. See the spec and multiline strings in Go.

go
package main

import "fmt"

func main() {
	fmt.Println(`\GoLinux\Cloud\`)
	fmt.Println(`GoLinux\\Cloud`)
	fmt.Println(`GoLinux\\\Cloud`)
}
Output

You should see backslashes printed exactly as written inside each raw literal.


Golang escape backtick in double quotes

There is no \"-style escape for grave accents inside ". Common fixes: use a raw string for the substring that contains `, or insert U+0060 with "\x60" / "\u0060" when you only need the character.

go
package main

import "fmt"

func main() {
	fmt.Println("run " + "\x60" + "go test" + "\x60" + " from the module root")
}
Output

You should see run go test from the module root with backticks around go test.


Summary

Golang escape string and go escape string work differently in interpreted double-quoted literals versus raw backtick literals: backslash starts escapes only in the former, so golang escape backslash is normally \\. Golang escape characters are the sequences listed in the language spec; invalid pairings cause compile errors, not a mysterious runtime crash. For heavy backslash or golang escape backtick needs, prefer raw strings or small rune escapes, and use multiline strings in Go when layout matters.


References


Frequently Asked Questions

1. How do I golang escape backslash in a normal string?

In a double-quoted interpreted string, write two backslashes so the first escapes the second: "\" produces one backslash in the string.

2. When should I use a raw string literal instead of escaping?

Use backtick raw strings for Windows paths, regex, or JSON fragments with many backslashes so you do not double every backslash; raw strings do not interpret escapes except the backtick itself.

3. How do I golang escape backtick inside a double-quoted string?

Go does not have a backslash-backtick escape in double quotes; use a raw string for the piece that contains backticks, or insert a grave accent with a rune or hex escape such as "\x60".

4. What golang escape characters exist in interpreted strings?

Common escapes include \n, \t, \r, \", \\, and Unicode \uXXXX or \UXXXXXXXX; anything else after a lone backslash is invalid or ends the string badly if the backslash is not paired before the closing quote.

5. Why does my string literal fail to compile with a backslash at the end?

A backslash at the end of an interpreted string starts an escape but there is no following character, so the compiler reports a syntax error; end the escape pair or switch to a raw string.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …