前回の記事でGo言語でパーセントエンコーディングを行ったので、今回はデコード編です。
とはいえ、標準でデコードするQueryUnescape()
関数が用意されているので、そちらを使うのみです。
前回のように半角スペースの置換を気にする必要はありません。
ドキュメントにも以下のように記載されています。
func QueryUnescape
converting %AB into the byte 0xAB and ‘+’ into ‘ ’ (space).
実装は以下です。
package main import ( "fmt" "net/url" "regexp" ) func main() { str := "パーセント エンコーディング" // encode str = url.QueryEscape(str) str = regexp.MustCompile(`([^%])(\+)`).ReplaceAllString(str, "$1%20") // decode str, _ = url.QueryUnescape(str) fmt.Println(str) // パーセント エンコーディング }