Javascript unescape equivalent in golang?

huangapple go评论96阅读模式
英文:

Javascript unescape equivalent in golang?

问题

在Golang中是否有JavaScript的unescape()函数的等效方法?

JavaScript示例:

package main

import (
	"fmt"
	"net/url"
)

func main() {
	c, _ := url.QueryUnescape("%u0107") // "ć"
	fmt.Println(c)
}

在Golang中,可以使用net/url包中的QueryUnescape()函数来实现与JavaScript的unescape()函数类似的功能。以上是一个示例代码,它将%u0107解码为ć并打印输出。

英文:

Is there an equivalent of JavaScript's unescape() in Golang?

JavaScript example:

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

答案1

得分: 1

unescape函数在Mozilla文档[1]中被标记为“已弃用”(红色垃圾桶)。因此,我不建议使用它,也不建议寻找Go语言中的等效函数。Go语言有一个类似的函数[2],但它对输入的要求与您提供的不同:

package main
import "net/url"

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

如果您真的坚持要这样做,您可以尝试翻译这个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
英文:

The unescape function is marked as "Deprecated" (red trash can), in the Mozilla documentation [1]. As such, I wouldn't 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("%C4%87")
   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

huangapple
  • 本文由 发表于 2021年10月24日 01:04:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/69690269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定