douye2036 2015-12-29 16:06
浏览 35
已采纳

从字面上输出unicode,而不是作为unicode输出

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条回答 默认 最新

  • doufengsui7449 2015-12-30 02:52
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 vs 创建windows 窗体应用(.net framework)项目时,出现问题,无法进入下一步
  • ¥15 如何实现安卓Socks5代理服务端,并且实现内网穿透?
  • ¥50 自有服务器搭建正向代理及负载均衡应对高并发
  • ¥15 Expected a list, got: <class 'list'>. Correct! 为什么它不输出答案而是答案的类型
  • ¥15 pbootcms筛选怎么调用出来
  • ¥15 开发板和电机具体的参数?
  • ¥100 如何对超大数据分段并进行对比处理
  • ¥50 mmc在一个管理单元检测到错误
  • ¥15 如何正确使用pyside6,使其符合LGPL?
  • ¥15 百度网盘app文件浏览效果怎么做?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部