我不清楚GO项目与GOPATH的关系。虽然我引用了很多案例。
我发现有两种解决方案
- 当有新项目需要添加时重置(或添加新的东西)GOPATH D:\workspace\golang\calcproj;D:\workspace\golang\golearning;D:\workspace\golang\rpcclient;D:\workspace\golang\rpcserver
- 只设置了一个永远只有一个的GOPATH,修改了import的代码 D:\workspace\golang
当我尝试按照解决方案 2 在 Window 操作系统中设置项目时,我总是收到错误信息:无法加载包:包计算:
我的GOPATH : E:\GoWorks ,Go Project文件目录如下:
我不喜欢方案1,因为每次有新项目的时候我们都需要修改GOPATH~~
Q1。但是如何像其他解决方案一样构建项目呢?
Q2。无论如何,GOPATH 是如何工作的?
Q3。使用 GOPATH 设置 GO 项目的最佳方式是什么
最佳答案
GOPATH
是如何工作的? GOPATH
是 go 寻找本地依赖项来构建应用程序的地方。这就像您正在编码的工作区的根命名空间。如果您更改代码位置,您可能也想更改您的 GOPATH
。
使用 GOPATH
设置 GO 项目的最佳方法是什么? This wiki article has your answer
自从我回答了这个问题后,我建议去那里并跟踪面包屑(如果它已移动)。但是,如果您正在阅读时它不可用,这是一个片段
Your workspace can be located wherever you like, but we'll use C:\work in this example. Note that GOPATH must not be the same path as your Go installation.
- Create folder at C:\work.
- Right click on "Start" and click on "Control Panel". Select "System and Security", then click on "System".
- From the menu on the left, select the "Advanced systems settings".
- Click the "Environment Variables" button at the bottom.
- Click "New" from the "User variables" section.
- Type GOPATH into the "Variable name" field.
- Type C:\work into the "Variable value" field.
- Click OK.
我认为对于您的情况,您要添加 C:\workspace\golang\...\projectname1;C:\workspace\golang\...\projectname2;...
而不是只需使用 C:\workspace
作为您的 GOPATH
。请记住,这假设您为所有项目使用一个公共(public)命名空间,因此您需要在 import< 中包含路径的
部分。如果您想排除包路径的 \golang\...
部分\golang\
部分,请将其包含在您的 GOPATH
中。
关于go - 如何使用 GOPATH 设置(或存储)Go 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148724/