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
Cannot use <variable> as <type> value in return statement error in Golang
Golang program for implementation of Interpolation Search
Example to use various String Functions in Go Language
Sierpinski Carpet in Go Programming Language
GO Program to Generate Fibonacci Sequence Up to a Certain Number
Is There Still a Need for Programming in This AI World?
Most Helpful This Week
Golang import package inside packageHow to check pointer or interface is nil?Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]Strip all white spaces, tabs, newlines from a stringExample: Fields and FieldsFunc from BYTES PackageHow to Draw a rectangle in Golang?Example to handle GET and POST request in GolangConvert specific UTC date time to PST, HST, MST and SGTExample: How to use ReadAtLeast from IO Package in Golang?How to set timeout for http.Get() requests in Golang?