doulv8162 2017-07-13 14:55
浏览 150
已采纳

在Golang中如何将punycode转换为unicode?

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条回答 默认 最新

  • dsxml2169 2017-07-13 15:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题