英文:
How to store unicode in a Go struct (golang)
问题
我正在使用golang读取Unicode名称。我的结构体项的类型是字符串,但我认为这是不正确的。
是否有一个示例可以正确地存储Unicode字符串在golang结构体中?
当我使用csv.NewReader读取CSV文档并将其打印到屏幕上时,它工作正常,但一旦作为字符串存储在结构体中,它就不再正确打印。这似乎是一个简单的字节/字符串问题,但我很难解决。
我尝试在结构体中使用[]byte,但是如何进行字符串的比较以及如何正确地将[]byte打印到文件中呢?由于我正在将其写入RDF文件,我怀疑我需要转换为UTF-8或其他编码方式。
英文:
I am reading in unicode names in golang. My struct item is of type string, but this not correct I believe.
Is there an example of how to properly store unicode strings in a golang struct?
When I read the CSV document using csv.NewReader and print it to screen it works fine, but once in the struct as a string, it no longer prints correctly. This seems like a simple byte / string issue but I am having a hard time resolving it.
I tried using []byte in the struct, but then how do I do comparisons of the strings laters and what is the way I would print that []byte to a file correctly? Since I am writing to file as RDF, I suspect I need to convert to UTF-8 or something?
答案1
得分: 3
Go期望字符串数据以UTF-8编码。如果你的输入数据使用了其他编码方式,你需要在将其赋值给字符串之前将其转换为UTF-8。
你可以手动进行转换,或者使用第三方库,比如go-charset。
英文:
Go expects string data to be encoded as UTF-8. If your input data uses a different encoding, you will need to convert it to UTF-8 before assigning it to a string.
You can do this manually, or use a third party library like go-charset
答案2
得分: 3
这取决于你对"unicode"的理解。在Go语言中,所有内容都应该是UTF-8编码的,包括string
数据类型,所以你可能不需要做任何特殊处理(只要你处理的是UTF-8编码的内容)。
[]byte
只是一系列字节,它对其中的数据是不透明的。你不需要做任何特殊处理就可以将它写入文件。
标准库中有unicode
、unicode/utf8
和unicode/utf16
包。如果需要的话,还有一个规范化包可以在这里找到:http://godoc.org/golang.org/x/text/unicode/norm
这篇博文可以比我们在这里回答的更深入地解释,并提供了一些更多资源的链接:http://blog.golang.org/strings
英文:
It depends on what you mean by "unicode". Everything in Go is expected to be UTF-8, including the string
datatype, so there's probably nothing you need to do (as long as you're dealing with UTF-8).
[]byte
is just a series of bytes. It opaque to the data that's in it. You don't need to do anything special to write it to a file.
The stdlib has the unicode
, unicode/utf8
, and unicode/utf16
packages. There's also a normalization package if you need it here: http://godoc.org/golang.org/x/text/unicode/norm
This blog post can explain it in more depth than we can answer here, and has some links to more resources: http://blog.golang.org/strings
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论