Issue
I want to translate an IDNA ASCII URL to Unicode.
package main
import (
"golang.org/x/net/idna"
"log"
)
func main() {
input := "https://xn---36-mddtcafmzdgfgpbxs0h7c.xn--p1ai"
idnaProfile := idna.New()
output, err := idnaProfile.ToUnicode(input)
if err != nil {
log.Fatal(err)
}
log.Printf("%s", output)
}
The output is: https://xn---36-mddtcafmzdgfgpbxs0h7c.рф
It seems the IDNA package only converts the TLD. Is there some option that can convert the full URL?
I need to get the same result as when I paste the ASCII URL into Chrome:
https://природный-источник36.рф
Solution
You simply need to parse the URL first:
package main
import (
"golang.org/x/net/idna"
"net/url"
)
func main() {
p, e := url.Parse("https://xn---36-mddtcafmzdgfgpbxs0h7c.xn--p1ai")
if e != nil {
panic(e)
}
s, e := idna.ToUnicode(p.Host)
if e != nil {
panic(e)
}
println(s == "природный-источник36.рф")
}
https://golang.org/pkg/net/url#Parse
Answered By – Zombo
Answer Checked By – Mary Flores (GoLangFix Volunteer)