draj840143 2018-08-26 12:32 采纳率: 100%
浏览 55
已采纳

访问go模块中的本地包(go 1.11)

I'm trying out Go's new modules system and am having trouble accessing local packages. The following project is in a folder on my desktop outside my gopath.

My project structure looks like:

/
  - /platform
      - platform.go
  - main.go
  - go.mod

// platform.go
package platform

import "fmt"

func Print() {
    fmt.Println("Hi")
}

// main.go
package main

import "platform"

func main() {
    platform.Print()
}

go build main.go tells me

cannot find module for path platform
  • 写回答

2条回答 默认 最新

  • douban2014 2018-08-26 13:08
    关注

    I would strongly suggest you to use go toolchain which takes care of these issues out of the box. Visual Studio Code with vscode-go plugin is really useful.

    Problem here is that Go requires relative paths with respect to your $GOPATH/src or module in import statement. Depending on where you are in your GOPATH, import path should include that as well. In this case, import statement must include go module path in go.mod

    GOPATH

    Assume your project resides here:

    $GOPATH/src/github.com/myuser/myproject
    

    Your import path should be:

    import "github.com/myuser/myproject/platform"
    

    VGO

    Assume your go.mod file is:

    module example.com/myuser/myproject
    

    Your import path should be:

    import "example.com/myuser/myproject/platform"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?