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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试