在Go语言中打印双引号(”)可以使用转义字符\”。

huangapple go评论67阅读模式
英文:

Printing " (double quote) in GoLang

问题

我正在编写一个读取文件的Go代码。为了做到这一点,我使用fmt.Println()将内容打印到中间文件中。

要打印"字符,你可以使用\"

英文:

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

答案1

得分: 43

这很简单,就像C语言一样。

fmt.Println("\"")
英文:

This is very easy, Just like C.

fmt.Println("\"")

答案2

得分: 37

旧式字符串字面量及其转义通常可以避免使用。典型的Go解决方案是在此处使用原始字符串字面量:

 fmt.Println(`"`)
英文:

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(`"`)

答案3

得分: 22

不要说Go没有给你选择的余地。以下所有的代码都会打印一个引号 "

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

// 真的,这个只是为了演示而不是用于生产 :)
fmt.Println(xml.Header[14:15])
fmt.Println(strconv.Quote("")[:1])

Go Playground上尝试它们。

英文:

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("2")
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.

答案4

得分: 10

  • fmt.Printf("test: %q", "bla")
  • 输出: test: "bla"
英文:
  • fmt.Printf("test: %q", "bla")
  • output: test: "bla"

huangapple
  • 本文由 发表于 2017年1月31日 17:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/41953577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定