Skip to main content

How to Write a Raw String Literal in Go

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A raw string literal in Go is text between backquotes. Inside it, nothing is special: double quotes, backslashes and line breaks are just characters. That makes it the easiest way to write JSON, Windows paths and regular expressions in Go code.

Syntax​

Syntax
<name> := `<text with "quotes" and \backslashes>`
PartWhat it does
`...`A raw string literal. Everything between the backquotes is taken as it is.
"..."An interpreted string literal, for comparison: backslash sequences such as \n and \" are turned into special characters.

The Go language specification on string literals: "Raw string literals are character sequences between back quotes, as in foo. Within the quotes, any character may appear except back quote."

The Example​

The example writes a small piece of JSON as a raw string, writes the Windows path C:\temp\new as a raw string, and compares that path with the same path written as an interpreted string, where each backslash has to be doubled.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which declares JSON in backquotes, the path C:\temp\new in backquotes, the same path in double quotes with doubled backslashes, then prints the JSON, the raw path and whether the two paths are equal.

main.go
package main

import "fmt"

func main() {
json := `{"name":"Ada","role":"admin"}`
path := `C:\temp\new`
escaped := "C:\\temp\\new"

fmt.Println(json)
fmt.Println(path)
fmt.Println(path == escaped)
}

Step 2: Run It​

Terminal
go run main.go
Output
{"name":"Ada","role":"admin"}
C:\temp\new
true

A zsh terminal where go run main.go prints the JSON with its double quotes, C:\temp\new, and true.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • The JSON printed its double quotes as they are, with no backslashes in the code.
  • In the raw path, \t and \n stayed as a backslash followed by a letter. In an interpreted string they would have become a tab and a line break.
  • "C:\\temp\\new" says the same thing with each backslash doubled, so the comparison printed true.

Common Mistakes​

MistakeWhat happensDo this instead
Writing a Windows path in double quotes with single backslashes\t becomes a tab and \n a line breakUse a raw string, or double each backslash
Expecting \n in a raw string to be a line breakIt stays a backslash and an nPut a real line break in the raw string, or use an interpreted string
Putting a backquote inside a raw stringIt ends the string thereWrite that part as an interpreted string and join the two with +

Troubleshooting​

SymptomLikely causeFix
A tab or a missing character in a printed pathThe path was in double quotes with single backslashesUse backquotes
A literal \n printed instead of a new lineThe text is a raw stringUse a real line break, or double quotes

FAQ​

Can a raw string span several lines?​

Yes. A line break inside backquotes is part of the string: a raw string with line one, a line break and line two printed two lines.

Is a raw string slower?​

No. Both kinds produce an ordinary string; they differ only in how the text is written in the code.

Conclusion​

Put text between backquotes in Go and it stays exactly as written: double quotes, backslashes and line breaks included. Use it for JSON, paths and regular expressions, and switch to double quotes when you need escape sequences or a backquote.

Additional Resources​