29

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 "?

icza
  • 389,944
  • 63
  • 907
  • 827
bender
  • 369
  • 1
  • 4
  • 9

4 Answers4

43

This is very easy, Just like C.

fmt.Println("\"")
Sourabh Bhagat
  • 1,691
  • 17
  • 20
37

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(`"`)
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
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.

icza
  • 389,944
  • 63
  • 907
  • 827
10
okhrypko
  • 101
  • 1
  • 3