如何在Go中将数据库中的Unicode字符串转换为UTF字符串?

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

How convert unicode string from database to utf-string in Go?

问题

字符串以Unicode形式存储在数据库中,例如"\u0435\u043e..."(表的编码为UTF-8)。

在从数据库中选择并打印后:

log.Println(str)

输出:\u0435\u043e...

我该如何将这个字符串转换为UTF表示形式?

英文:

String is stored in database as unicode "\u0435\u043e ..." (table's encoding is UTF-8).

After selecting from database and printing:

log.Println(str)

OUT: \u0435\u043e...

How can I transform this string to utf presentation?

答案1

得分: 2

要解码你所拥有的字符串,你可以这样做:

import "net/url"

...

url.QueryUnescape("\u0435\u043e")

但我认为你的数据库或连接参数存在错误配置,因为这应该会自动处理。顺便说一下,这不是UTF-8编码。

英文:

To decode the string you have, you can do:

import "net/url"

...

url.QueryUnescape("\u0435\u043e")

But I think there's a misconfiguration of your database or connection parameters, as this should be handled automatically. This is not utf-8 BTW.

huangapple
  • 本文由 发表于 2015年9月25日 20:29:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/32782239.html
匿名

发表评论

匿名网友

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

确定