Printing " (double quote) in GoLang
Asked Answered
M

4

30

I am writing a Go code which reads from a file. To do so I use fmt.Println() to print into that intermediate file.

How can I print "?

Mohur answered 31/1, 2017 at 9:12 Comment(0)
H
45

This is very easy, Just like C.

fmt.Println("\"")
Hennessy answered 31/1, 2017 at 9:13 Comment(1)
More easily even fmt.Println(`"`)Hackery
P
40

Old style string literals and their escapes can often be avoided. The typical Go solution is to use a raw string literal here:

 fmt.Println(`"`)
Punish answered 31/1, 2017 at 9:15 Comment(0)
M
22

Don't say Go doesn't leave you options. The following all print a quotation mark ":

fmt.Println("\"")
fmt.Println("\x22")
fmt.Println("\u0022")
fmt.Println("\042")
fmt.Println(`"`)
fmt.Println(string('"'))
fmt.Println(string([]byte{'"'}))
fmt.Printf("%c\n", '"')
fmt.Printf("%s\n", []byte{'"'})

// Seriously, this one is just for demonstration not production :)
fmt.Println(xml.Header[14:15])
fmt.Println(strconv.Quote("")[:1])

Try them on the Go Playground.

Matteo answered 31/1, 2017 at 9:36 Comment(0)
E
10
Explant answered 16/2, 2018 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.