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
Find out how many logical processors used by current process
Golang program for implementation of Radix Sort
Database as a Service (DBaaS): Simplifying Database Management in the Cloud
Golang Program to Triangle of Alphabets
Golang write CSV records
Example of Sscan vs Sscanf vs Sscanln from FMT Package
Most Helpful This Week
Find capacity of Channel, Pointer and SliceExample: Stack and Caller from RUNTIME packageHow to read input from console line?How to write backslash in Golang string?How to get struct variable information using reflect package?How to check pointer or interface is nil?Golang Slice interface and array concatenationHow to declare empty Map in Go?Pass different types of arguments in variadic functionExample: How to use TeeReader from IO Package in Golang?