dongyu9263 2019-05-22 14:31
浏览 602

如何使用Go Importer

I have been trying to use the importer to parse the types defined in a particular package. However, the importer always return an error saying the package is not found. What mistake I am making?

package main

import (
    "fmt"
    "go/importer"
)

func main() {
    pkg, err := importer.Default().Import("github.com/onsi/ginkgo")
    if err != nil {
        panic(err)
    }
    fmt.Println(pkg)
}

I tried to read the go importer documentation, but it provides very limited information. I also tried to use the package I am importing here, but it does not help. However, if I import a go standard package, such as “time”, I can currently import the package. Why is that?

  • 写回答

1条回答 默认 最新

  • duanfen9090 2019-05-22 14:49
    关注

    Go importer will not download the package for you. You can use dep or go modules to handle your dependencies, but an easy fix would be downloading the package directly to your gopath using go get:

    go get -u github.com/onsi/ginkgo
    

    After that, go importer will work and your code output should be:

    package ginkgo ("github.com/onsi/ginkgo")
    

    [EDIT] Using Go Modules:

    There's a bunch of tutorials about that, but the quick and dirty way is, on your package directory:

    $ GO111MODULE=on go mod init
    $ GO111MODULE=on go mod tidy
    

    That will check your project and download all packages. To install a specific package on your go.mod, you can use:

    $ go install github.com/onsi/ginkgo
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私