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
Regular expression to validate email address
Regular expression to extract DNS host-name or IP Address from string
Functions mess (recursive anonymous function) in Golang
How to compare equality of struct, slice and map?
How to declare and access pointer variable?
Golang Get current Date and Time in EST, UTC and MST?
How do you write multi-line strings in Go?
How to check if a string contains a numbers in Golang?
Regular expression to validate the date format in "dd/mm/yyyy"
How to get first and last element of slice in Golang?
Most Helpful This Week
How to convert Go struct to JSON?Data encryption with AES-GCMHow to check pointer or interface is nil?Regular expression to extract numbers from a string in GolangHow to get current IP form ipify.org ?How to print struct variables data in Golang?How to read input from console line?How to create a photo gallery in Go?How to Convert string to integer type in Go?Convert Float32 to Float64 and Float64 to Float32