string 在 GoLang 中打印 "(双引号)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41953577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing " (double quote) in GoLang
提问by bender
I am writing a Go code which reads from a file. To do so I use fmt.Println()
to print into that intermediate file.
我正在编写一个从文件中读取的 Go 代码。为此,我使用fmt.Println()
打印到该中间文件中。
How can I print "
?
如何打印"
?
回答by Sourabh Bhagat
This is very easy, Just like C.
这很容易,就像C一样。
fmt.Println("\"")
回答by Denys Séguret
Old style string literals and their escapes can often be avoided. The typical Go solution is to use a raw string literalhere:
通常可以避免旧式字符串文字及其转义。典型的 Go 解决方案是在此处使用原始字符串文字:
fmt.Println(`"`)
回答by icza
Don't say Go doesn't leave you options. The following all print a quotation mark "
:
不要说 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{'"'})
// 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.
在Go Playground上试一试。
回答by okhrypko
- fmt.Printf("test: %q", "bla")
- output: test: "bla"
- play ground here
- docs here