访问go模块中的本地包(go 1.11) [英] Accessing local packages within a go module (go 1.11)

查看:69
本文介绍了访问go模块中的本地包(go 1.11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用Go的新模块系统,无法访问本地软件包.以下项目位于我的gopath之外的桌面上的文件夹中.

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.

我的项目结构如下:

/
  - /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告诉我

cannot find module for path platform

推荐答案

我强烈建议您使用go工具链,它可以解决这些问题.带有vscode-go插件的Visual Studio Code非常有用.

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.

这里的问题是Go需要相对于import语句中的$GOPATH/srcmodule的相对路径.根据您在GOPATH中的位置,导入路径也应包括该内容.在这种情况下,import语句必须在go.mod

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

您的导入路径应为:

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

VGO

假设您的go.mod文件为:

Assume your go.mod file is:

module example.com/myuser/myproject

您的导入路径应为:

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

这篇关于访问go模块中的本地包(go 1.11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆