How in Golang convert punycode to unicode?

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

How in Golang convert punycode to unicode?

问题

我想要转换URL,例如:

> xn--h1aaebtrh5b.xn--p1ai --> кисточки.рф

当然也可以反向转换:

> kremlin.ru ---> xn--d1abbgf6aiiy.xn--p1ai

我尝试使用idna包,但无法导入供应商包idna

链接在文档中:
https://godoc.org/golang.org/x/net/idna#Profile.ToUnicode

尝试导入:

import "golang_org/x/net/idna"

出现错误:

main.go:18:8: 在任何位置都找不到包“golang_org/x/net/idna”(来自$GOROOT)
/home/arseny/go/src/golang_org/x/net/idna(来自$GOPATH)

另一种尝试:

import "vendor/golang_org/x/net/idna"

错误:

main.go:18:8: 必须导入为golang_org/x/net/idna
main.go:19:2: 不允许使用供应商包
英文:

I wanna convert url f.e.:

> xn--h1aaebtrh5b.xn--p1ai --> кисточки.рф

And of course reverse:

> kremlin.ru ---> xn--d1abbgf6aiiy.xn--p1ai

I try use idna package, but i can't import vendor package idna

Link on doc:
https://godoc.org/golang.org/x/net/idna#Profile.ToUnicode

Try import:

import "golang_org/x/net/idna"

Get error:

main.go:18:8: cannot find package "golang_org/x/net/idna" in any of:
	/usr/local/go/src/golang_org/x/net/idna (from $GOROOT)
	/home/arseny/go/src/golang_org/x/net/idna (from $GOPATH)

Another try:

import "vendor/golang_org/x/net/idna"

Errors:

main.go:18:8: must be imported as golang_org/x/net/idna

main.go:19:2: use of vendored package not allowed

答案1

得分: 2

有些人说这段代码有效。

在bash中:

go get golang.org/x/net/idna

示例代码go:

package main

import (
    "fmt"
    "golang.org/x/net/idna"
)

var p *idna.Profile

func main() {
    // Raw Punycode没有限制,也不进行映射。
    p = idna.New()
    fmt.Println(p.ToUnicode("xn--d1abbgf6aiiy.xn--p1ai"))
}

但我仍然希望存在另一种方式。我不喜欢将包的本地副本复制到$GOROOT路径中。

英文:

Some people say that this code work

In bash:

go get golang.org/x/net/idna

Example code go:

package main

import (
    "fmt"
    "golang.org/x/net/idna"
)

var p *idna.Profile

func main() {
    // Raw Punycode has no restrictions and does no mappings.
    p = idna.New()
    fmt.Println(p.ToUnicode("xn--d1abbgf6aiiy.xn--p1ai"))
}

But i'm still hope that another way exist. I unlike to do local copy of package into $GOROOT path.

huangapple
  • 本文由 发表于 2017年7月13日 22:55:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/45084301.html
匿名

发表评论

匿名网友

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

确定