go实现URL encode编码和解码用到了​​net/url​​标准库的QueryEscape(用来编码)和QueryUnescape(用来解码)。

package main

import (
"fmt"
"net/url"
)

func main() {
urlStr := "https://www.test.com?key=中国#test1"
escapeUrl := url.QueryEscape(urlStr)
fmt.Println("URL encode编码:", escapeUrl)

enEscapeUrl, _ := url.QueryUnescape(escapeUrl)
fmt.Println("URL encode解码:", enEscapeUrl)
}

go实现urlencode编码和解码_开发语言

JavaScript实现

encodeURIComponent("http://www.baidu.com/中国")
decodeURIComponent('http%3A%2F%2Fwww.baidu.com%2F%E4%B8%AD%E5%9B%BD')

go实现urlencode编码和解码_后端_02

参考链接:​​https://pkg.go.dev/net/url@go1.19.3#example-QueryEscape​