英文:
use custom Go packages
问题
我写了一些自定义的包,但我在重用它们时遇到了困难。
我的代码结构如下:
$GOPATH/src/github.com/myusername/myfirstrepo/
在myfirstrepo中,我有两个不同的包:
myfirstrepo
|--somefolder1/package1
|--somefolder2/package2
现在我想在package2中导入我的package1,我知道我可以使用go get来使用我在GitHub上发布的代码,但我想知道是否可以使用类似于npm的东西:
import "./../somefolder1/package1"
显然,我已经尝试过这个方法,但它不起作用。
我应该如何简单地做到这一点?
英文:
I have written some custom packages which I am struggling to reuse.
My code is structured as follows:
$GOPATH/src/github.com/myusername/myfirstrepo/
In myfirstrepo I have 2 different packages:
myfirstrepo
|--somefolder1/package1
|--somefolder2/package2
Now I'd like to import my package1 in package2, I know I can use my published code on github using go get but I'd like to know if it's possible to use something similar to npm:
import "./../somefolder1/package1"
Obviously I've tried this and it doesn't work.
How can I simply do this?
答案1
得分: 2
现在我想在package2中导入我的package1,我知道我可以使用go get来使用我在GitHub上发布的代码。
它不一定要在GitHub上发布。
导入路径只需要是您**工作区**($GOPATH)中的有效路径。
import "github.com/myusername/myfirstrepo/somefolder1/package1"
相对路径不是最佳实践(除非您真的需要在没有分类器的情况下访问函数)。
英文:
> Now I'd like to import my package1 in package2, I know I can use my published code on github using go get
It doesn't have to be published on github.
The import path just has to be a valid path within your workspace ($GOPATH)
import "github.com/myusername/myfirstrepo/somefolder1/package1"
Relative paths are not the best practice (unless you really need to access your functions without classifier).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论