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?
How to Convert string to integer type in Go?
Closures Functions in Golang
Select single argument from all arguments of variadic function
Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of Slices
How to fix race condition using Atomic Functions in Golang?
Most Helpful This Week
Golang Functions Returning Multiple ValuesHow To Make HTTP Requests in Go?Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.Strip all white spaces, tabs, newlines from a stringHow To Make an HTTP Server in Golang?Different ways to convert Byte Array into StringCatch values from GoroutinesHow to check if a string contains a white space in Golang?Convert Int data type to Int16 Int32 Int64Golang String Concatenation