How to print string with double quote in Go?
There are different tricky ways in Golang to print a string with double-quotes.
Printing String Using (%q)
In the below example, we used %q to display a string with double-quotes.
Example
package main
import "fmt"
func main() {
var lang = "Golang"
fmt.Printf("%q", lang)
}
Output
"Golang"
Printing String Using an escape character (\)
In the below example, we used \ to print a string with double-quotes. We just added the \ before the double-quotes.
Example
package main
import "fmt"
func main() {
var lang = "\"Golang\""
fmt.Println(lang)
}
Output
"Golang"
Printing String Using a Raw String Lateral (`)
In the below example, we used a raw string lateral ` to print a string with double-quotes. You can also use this way to print a string with single-quotes.
Example
package main
import "fmt"
func main() {
var lang = `"Golang"`
fmt.Println(lang)
lang = `'Golang'`
fmt.Println(lang)
}
Output
"Golang"
'Golang'
Most Helpful This Week
Example: ReadAll, ReadDir, and ReadFile from IO Package
Go program to find TXT records of a domain
Cannot call non-function <variable> error in Golang
How do you handle HTTP server shutdown gracefully in Go?
Golang Program to print Floyd's triangle
Undefined reference to <variable/function> error in Golang
Different ways for Integer to String Conversions
How to create Slice using Make function in Golang?
Write string slice line by line to a text file
Golang program for implementation of Random Maze Generator
Most Helpful This Week
Regular expression to validate email addressRegular expression to extract all Non-Alphanumeric Characters from a StringHow to create Map using the make function in Go?How to convert Boolean Type to String in Go?Golang String ConcatenationHow to reads and decodes JSON values from an input stream?Golang download image from given URLHow to count number of repeating words in a given String?How to print string with double quote in Go?How to import structs from another package in Go?