You can use the strconv
package to parse the string literal containing the escape sequences.
The quick and dirty way is to simply add the missing quotes and interpret it as a quoted string using strconv.Unquote
s := `\xe2\x96\xb2`
s, err := strconv.Unquote(`"` + s + `"`)
You can also directly parse the string one character at a time (which is what Unquote
does internally), using strconv.UnquoteChar
s := `\xe2\x96\xb2`
buf := make([]byte, 0, 3*len(s)/2)
for len(s) > 0 {
c, _, ss, err := strconv.UnquoteChar(s, 0)
if err != nil {
log.Fatal(err)
}
s = ss
buf = append(buf, byte(c))
}
s = string(buf)