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
How to iterate over a Map using for loop in Go?
Regular expression to extract all Non-Alphanumeric Characters from a String
Example: How to use ReadAtLeast from IO Package in Golang?
How to append text to a file in Golang?
How to add Watermark or Merge two image?
Converting Int data type to Float in Go
Most Helpful This Week
Convert Float32 to Float64 and Float64 to Float32Regular expression to extract text between square bracketsGet Set and Clear Session in GolangExample: How to use ReadAtLeast from IO Package in Golang?How to get Dimensions of an image type jpg jpeg png or gif ?How to use a mutex to define critical sections of code and fix race conditions?Split URL and Get Parameters from URLRegular expression to extract numbers from a string in GolangWhat is GOPATH?Golang Get current Date and Time in EST, UTC and MST?