dsa99349 2018-06-11 18:30
浏览 277

Golang:如何使用vgo? 错误:“找不到包”

  1. I have a project which is currently under my GOPATH /usr/local/go/src/

  2. Then I went inside my project root and ran: go get -u golang.org/x/vgo

  3. In my main.go I want to use lib httprouter so I added in import statement:

    import ( "github.com/julienschmidt/httprouter" )

  4. I ran vgo build

  5. Then when I start to run my server as usual I get the following error:

main.go:8:2: cannot find package "github.com/julienschmidt/httprouter" in any of: /usr/local/go/src/github.com/julienschmidt/httprouter (from $GOROOT) /Users/myuser/go/src/github.com/julienschmidt/httprouter (from $GOPATH)

I understand the error, it seems to be searching for httprouter under GOPATH or GOROOT. So do I have to do anything else to just let vgo do its thing and I can run my server successfully?

  • 写回答

1条回答 默认 最新

  • dongrenshi0889 2018-06-12 14:03
    关注

    Using vgo build

    Here is a "Hello, World" walk-through of using vgo, including common errors and how to respond to them.

    1. $ go get -u golang.org/x/vgo
    2. $ cd /tmp/hello # Outside of GOPATH.
    3. $ cat main.go

      package main
      
      import "fmt"
      
      func main() {
          fmt.Println("Hello, World")
      }
      
    4. $ vgo build

      cannot determine module root; please create a go.mod file there
      
    5. This error tells us we need to specify what the root of our vgo module is. A module is a collection of packages (possibly one) versioned together. Create an empty go.mod file to signify this is the module root:

      $ touch go.mod
      
    6. Try building again: $ vgo build.

      vgo: cannot determine module path for source directory
      /tmp/hello (outside GOPATH, no import comments)
      
    7. This error tells us vgo doesn't know the import path of our package. There are two ways to tell it:

      1. Add a module statement to go.mod.
      2. Add an import comment to our package.

      We'll go with option 2 right now and come back to option 1. Change the first line of main.go to be:

      package main // import "my/import/path/hello"
      
    8. $ vgo build one last time.

    9. Run the binary: $ ./hello

      Hello, World
      

    Note: go.mod now contains a module line (option 1 above):

    $ cat go.mod 
    module my/import/path/hello
    

    If you are inside your GOPATH, the error in step 5 should not occur - vgo build should automatically figure out you're inside your GOPATH and add the module line accordingly.


    Using vgo run (without using vgo build)

    For this example, lets use the hello.go from the A Tour of Versioned Go (vgo):

    1. $ go get -u golang.org/x/vgo
    2. $ mkdir /tmp/hello2
    3. $ curl -sS https://swtch.com/hello.go >hello.go
    4. $ vgo run hello.go

      cannot determine module root; please create a go.mod file there
      
    5. $ touch go.mod

    6. $ vgo run hello.go

      vgo: resolving import "rsc.io/quote"
      vgo: finding rsc.io/quote (latest)
      vgo: adding rsc.io/quote v1.5.2
      Hello, world.
      
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制