Home > other >  Javascript unescape equivalent in golang?
Javascript unescape equivalent in golang?

Time:10-24

Is there an equivalent of Javascript's unescape() in golang?

Javscript example:

<script>
var c = unescape('%u0107');     // "ć"
console.log(c);
</script>

CodePudding user response:

The unescape function is marked as "Deprecated" (red trash can), in the Mozilla documentation [1]. As such, I wouldnt recommend using it, and certainly not seeking out an equivalent Go function. Go has a similar function [2], but it expects different input from what you have provided:

package main
import "net/url"

func main() {
   s, err := url.PathUnescape("ć")
   if err != nil {
      panic(err)
   }
   println(s == "ć")
}

If you are really set on doing this, you could see about translating this polyfill [3].

  1. https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/unescape
  2. https://godocs.io/net/url#PathUnescape
  3. https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.unescape.js
  • Related