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条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答