Search code examples
goirc

unicode being output literally instead of as 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?


Solution

  • 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
    }