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
Unveiling the Potential of Quantum Computing in AI: A Game Changer for Industries
Pass an Interface as an argument to a function in Go (Golang)
Golang program for implementation of Knuth–Morris–Pratt (KMP) Algorithm
Illustration of Checkpoint Synchronization in Golang
Write string slice line by line to a text file
Golang program for implementation of Linked List
10 Countries Paying Highest Salaries to Cybersecurity Professionals
Read and Write Fibonacci series to Channel in Golang
Example to compare Println vs Printf
Golang program for implementation of Comb Sort
Most Helpful This Week
Various examples of Carbon date-time package in GolangHow to extract text from between html tag using Regular Expressions in Golang?Constructors in GolangHow to use Ellipsis (...) in Golang?Example: How to use ReadAtLeast from IO Package in Golang?How to use for and foreach loop?Example: Split, Join, and Equal from BYTES PackageReplace numbers by zero from stringHow to read current directory using Readdir?Regular expression to extract text between square brackets