To print or create a string with double quotes in Go, you can use any of the following methods:
- Format a string using
%q
verb in thefmt.Printf()
orfmt.Sprintf()
functions. - Format a string using escaped double quotes
\"
in thefmt.Printf()
orfmt.Sprintf()
functions. - Use the
strconv.Quote()
function. - Format a double-quoted string using a raw string literal - a string between back quotes
`
in thefmt.Printf()
orfmt.Sprintf()
functions.
See the examples below to learn how each of these methods works.
Print a string with double quotes using the %q
format verb
The easiest method of creating of printing string with double quotes is to use the fmt.Printf()
or fmt.Sprintf()
functions with the %q
format verb, which is designed to do just that. From the fmt
package documentation:
%q
a double-quoted string safely escaped with Go syntax
Example:
package main
import "fmt"
func main() {
str := "Hello https://gosamples.dev"
// print string with double quotes
fmt.Printf("%q\n", str)
// create string with double quotes
quoted := fmt.Sprintf("%q", str)
fmt.Println(quoted)
}
Print a string with double quotes using the format with escaped double quotes
Alternatively, to print or create a string with double quotes, you can add these quotes manually in the format of the fmt.Printf()
or fmt.Sprintf()
functions. Note that the quotes within the string must be escaped by adding a backslash character before the quotation mark: \"
.
Example:
package main
import "fmt"
func main() {
str := "Hello https://gosamples.dev"
// print string with double quotes
fmt.Printf("\"%s\"\n", str)
// create string with double quotes
quoted := fmt.Sprintf("\"%s\"", str)
fmt.Println(quoted)
}
Create a string with double quotes using the strconv.Quote()
function
It is also possible to use the function strconv.Quote()
to create a new string with double quotes. In this case, printing and creating look almost the same because you need to create the quoted string first, and then you can print or use it in the application.
Example:
package main
import (
"fmt"
"strconv"
)
func main() {
str := "Hello https://gosamples.dev"
// print string with double quotes
fmt.Println(strconv.Quote(str))
// create string with double quotes
quoted := strconv.Quote(str)
fmt.Println(quoted)
}
Print a string with double quotes using back quotes (raw string) format
The last, most human-friendly method for creating or printing string with double quotes is to use the raw string literal in the format of the fmt.Printf()
or fmt.Sprintf()
functions. The raw string literal is any character sequence between back quotes, such as `abc`
. Strings between back quotes are printed as they are, they may contain newlines, and escape characters are not interpreted.
So in our case, we do not have to use the escaped double-quote character as in the second example. The only problem is when we want to print the quoted string with the newline at the end. Look at the example below. Since we are printing the raw string, we have to use Enter
to add a newline character which makes the code look ugly. For this reason, we recommend using the second method rather, with the double quotes escaped.
Example:
package main
import "fmt"
func main() {
str := "Hello https://gosamples.dev"
// print string with double quotes
fmt.Printf(`"%s"
`, str)
// create string with double quotes
quoted := fmt.Sprintf(`"%s"`, str)
fmt.Println(quoted)
}
All of the examples return the same output 😉:
"Hello https://gosamples.dev"
"Hello https://gosamples.dev"