使用自定义的Go包。

huangapple go评论79阅读模式
英文:

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

huangapple
  • 本文由 发表于 2015年1月22日 02:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/28074013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定