Skip to content

Instantly share code, notes, and snippets.

@spicydog
Last active June 22, 2024 10:56
Show Gist options
  • Save spicydog/54703add01e82e3c071482c2ce4e7c22 to your computer and use it in GitHub Desktop.
Save spicydog/54703add01e82e3c071482c2ce4e7c22 to your computer and use it in GitHub Desktop.
package main
import "encoding/json"
// Unescape UTF-8 string
// e.g. convert "\u0e27\u0e23\u0e0d\u0e32" to "วรญา"
func UnescapeString(str string) (unescapedString string) {
json.Unmarshal([]byte(`"`+str+`"`), &unescapedString)
return
}
@jairforero
Copy link

rubenhorn excelent solutions very thanks! (y)

@klingtnet
Copy link

klingtnet commented Jun 22, 2024

There's no need to rely on json.Unmarshal, instead you can use strconv.Unquote directly like so (playground):

source := "\u0e27\u0e23\u0e0d\u0e32"
converted, _ := strconv.Unquote(`"` + source + `"`)
fmt.Println(converted)
// prints วรญา

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment