Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

✌️ 4 ways to create or print string with double quotes in Go

introduction strings

Please consider supporting us by disabling your ad blocker

To print or create a string with double quotes in Go, you can use any of the following methods:

See the examples below to learn how each of these methods works.

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)
}

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)
}

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"

🎲 Generate a random string in Go

Learn how to generate a random string of a fixed length
introduction random strings

🍒 Concatenate strings in Go

Learn the differences between string concatenation methods
introduction strings

🖨️ Convert string to []byte or []byte to string in Go

Learn the difference between a string and a byte slice
introduction strings slice