Whenever the user wants to double-quote a string, he can’t simply write the string within double quotes inside the fmt.Printf() command. This prints only the text written inside those quotes. To print the string along with quotes, he can use various methods including certain escape characters. There are different ways in Golang to print a string with double-quotes.
1) Quoting a string using %q:
Syntax:
fmt.Printf("%q", output)
package main
import "fmt"
func main() {
var result = "Welcome to GeeksforGeeks."
fmt.Printf( "%q" , result)
}
|
Output:
"Welcome to GeeksforGeeks."
Explanation: In the above example, we used “%q” to display our string with double-quotes.
2) Quoting a string using an escape character “\”:
Syntax:
fmt.Println("\"string\"")
package main
import "fmt"
func main() {
fmt.Println( "\"GeeksforGeeks is a computer science portal\"" )
}
|
Output:
"GeeksforGeeks is a computer science portal"
Explanation: In the above example, we used an escape character “\” to print a string with double-quotes. We can simply do so by adding a backslash(\) before the double-quotes.
3) Double quoting a string using a raw string lateral (`):
Syntax:
fmt.Println(`"string\"`)
package main
import "fmt"
func main() {
fmt.Println(` "GeeksforGeeks" `)
}
|
Output:
"GeeksforGeeks"
Explanation: In the above example, we used a raw string lateral (`) to print a string with double-quotes.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 May, 2020
Like Article
Save Article