英文:
How to store text internally in a Go program?
问题
“软件应该在内部只使用Unicode字符串,在输出时转换为特定的编码。”
-- Python文档
上述引用来自Python文档。Python有一个Unicode字符串类型,所以这是有道理的。Go语言没有Unicode字符串,因为字符串只是一个不可变的字节切片。对于Go语言来说,等效的引用是什么呢?
是不是将文本在程序输入时转换为UTF-8,并在内部以UTF-8格式存储,然后输出为UTF-8格式?
英文:
> Software should only work with Unicode strings internally, converting to a particular encoding on output.
-- Python Docs
The above quote is from the Python docs. Python has a unicode string type so this makes sense. Go doesn't have unicode strings. As strings are just an immutable byte slice. What would be the equivalent quote for Go?
Would it be to convert text to utf-8 on entry to the program and store as utf-8 internally, and then output utf-8?
答案1
得分: 6
通常情况下,在Go语言中,你会像使用ioutil
包的WriteFile
方法时一样,编写一个[]byte
类型的变量。具体可以参考这个链接:https://golang.org/pkg/io/ioutil/#WriteFile
所以,是的,答案是你需要显式地声明编码方式。由于字符串只是一个字节切片,它没有固有的编码方式,但是在Go源代码中的字符串字面量将会是UTF-8编码的。如果你还没有阅读过Robert Pike在Go博客上关于字符串、字节和符文的文章,那么这篇文章值得一读:https://blog.golang.org/strings
英文:
Generally speaking, in Go you will be writing a []byte
like when using the ioutil
package's WriteFile
method; https://golang.org/pkg/io/ioutil/#WriteFile
So yes, the answer is that you explicitly declare the encoding. Since the string is just a byte slice, there is no inherent encoding, however string literals in Go source will be UTF-8. If you haven't already read this Go blog post by Robert Pike on strings, bytes and runes, it's worth the time; https://blog.golang.org/strings
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论