英文:
unicode being output literally instead of as unicode
问题
我正在使用Go创建一个IRC机器人作为我的第一个项目,以便熟悉这种语言。机器人的一个功能是从TVmaze API获取数据并在频道中显示。
我已经导入了一个env包,允许机器人管理员定义输出的显示方式。
例如,SHOWSTRING="#showname# - #status# - #network.name#"
我正在尝试添加功能,以便管理员可以使用IRC格式化功能,例如\u0002这是粗体\u0002
。
我有一个函数生成要返回并显示在频道中的字符串。
func generateString(show Show) string {
str := os.Getenv("SHOWSTRING")
r := strings.NewReplacer(
"#ID#", string(show.ID),
"#showname#", show.Name,
"#status#", show.Status,
"#network.name#", show.Network.Name,
)
result := r.Replace(str)
return result
}
根据我所了解的,我认为我需要使用rune
数据类型而不是字符串,然后在输出之前将符文转换为字符串。
我正在使用https://github.com/thoj/go-irceven包与IRC进行交互。
尽管我认为使用rune
是正确的方法,但我尝试了一些让我困惑的事情。
如果我将\u0002
添加到来自env的SHOWSTRING中,它会返回\u0002House\u0002 - Ended - Fox
。我是通过con.Privmsg(roomName, tvmaze.ShowLookup('house'))
来实现的。
然而,如果我尝试con.Privmsg(roomName, "\u0002这应该是粗体\u0002")
,它会输出粗体文本。
这里最好的选择是什么?如果是将字符串转换为符文,然后再转换回字符串,我该如何做到这一点?
英文:
I am creating an IRC bot using Go as a first project to get to grips with the language. One of the bot functions is to grab data from the TVmaze API and display in the channel.
I have imported an env package which allows the bot admin to define how the output is displayed.
For example SHOWSTRING="#showname# - #status# – #network.name#"
I am trying to add functionality to it so that the admin can use IRC formatting functionality which is accessed with \u0002 this is bold \u0002
for example.
I have a function which generates the string that is being returned and displayed in the channel.
func generateString(show Show) string {
str := os.Getenv("SHOWSTRING")
r := strings.NewReplacer(
"#ID#", string(show.ID),
"#showname#", show.Name,
"#status#", show.Status,
"#network.name#", show.Network.Name,
)
result := r.Replace(str)
return result
}
From what i have read i think that i need to use the rune
datatype instead of string and then converting the runes into a string before being output.
I am using the https://github.com/thoj/go-irceven package for interacting with IRC.
Although i think that using rune
is the correct way to go, i have tried a few things that have confused me.
If i add \u0002
to the SHOWSTRING from the env, it returns \u0002House\u0002 - Ended - Fox
. I am doing this by con.Privmsg(roomName, tvmaze.ShowLookup('house'))
However if i try con.Privmsg(roomName, "\u0002This should be bold\u0002")
it outputs bold text.
What is the best option here? If it is converting the string into runes and then back to a string, how do i go about doing that?
答案1
得分: 0
我需要在函数中对返回值使用strconv.Unquote()
。
现在,新的generateString
函数输出正确的字符串,代码如下:
func generateString(show Show) string {
str := os.Getenv("SHOWSTRING")
r := strings.NewReplacer(
"#ID#", string(show.ID),
"#showname#", show.Name,
"#status#", show.Status,
"#network.name#", show.Network.Name,
)
result := r.Replace(str)
ret, err := strconv.Unquote(`"` + result + `"`)
if err != nil {
fmt.Println("解引用字符串时出错")
}
return ret
}
英文:
I needed to use strconv.Unquote()
on my return in the function.
The new generateString function now outputs the correct string and looks like this
func generateString(show Show) string {
str := os.Getenv("SHOWSTRING")
r := strings.NewReplacer(
"#ID#", string(show.ID),
"#showname#", show.Name,
"#status#", show.Status,
"#network.name#", show.Network.Name,
)
result := r.Replace(str)
ret, err := strconv.Unquote(`"` + result + `"`)
if err != nil {
fmt.Println("Error unquoting the string")
}
return ret
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论