在 GitHub 存储库的子目录中获取包。

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

go get package in a subdirectory of a github repo

问题

如果你有一个仅包含Go代码的仓库(假设是https://github.com/foo/bar),你可以将代码直接放在根目录下,其他人可以使用以下命令导入:

go get -u github.com/foo/bar

目录和子目录将被识别为包。

但是,假设你正在创建一个多语言的GitHub仓库(例如,其中包含Java代码、Go代码、JavaScript代码等),并且你希望让其他人可以使用你的Go代码。通常情况下,从代码组织的角度来看,将Go代码放在根目录下是不可取的。更常见的做法可能是:

(根目录)
  |-- src
       |-- main
            |-- go
            |-- java
            |-- javascript
            |-- proto

现在,你的Go代码的根目录是https://github.com/foo/bar/src/main/go。但是,你不能再使用之前的go get命令,因为它会查找顶层目录。你也不想使用go get -u github.com/foo/bar/src/main/go,因为这样所有的包名都会包含在URL中,这样做很糟糕。

问题是,是否有一种方法可以指定精确的URL,以便你可以分离包名和URL?换句话说,我正在寻找类似于以下命令的东西:

go get -u github.com/foo/bar https://github.com/foo/bar/src/main/go

(当然,包名应该与源代码中实际存在的包名匹配,否则它将无法编译)。

我查看了Go文档,但我不确定是否可行。

英文:

If you have a go-only repo (let's say it's https://github.com/foo/bar), you can put your code right at the root level, and have others import using the following command:

go get -u github.com/foo/bar

Directories and subdirectories therein would be recognized as packages.

But suppose you are creating a multi-language repo on github (e.g. one that has java code, go code, javascript code, etc.). And you want to make your go code available for others to use. Usually putting the go code right at the root level would be a no-no from the code organization standpoint. What might be more common may be

(root)
  |-- src
       |-- main
            |-- go
            |-- java
            |-- javascript
            |-- proto

The root level of your go code is now at https://github.com/foo/bar/src/main/go. But you can no longer use the previous go get command as it would look at the top level. You don't really want to use go get -u github.com/foo/bar/src/main/go either as all those would now become part of the package names which is real yucky.

Question is, is there a way to specify an exact URL so that you can separate the package names and the URL? In other words, I'm looking for something like

go get -u github.com/foo/bar https://github.com/foo/bar/src/main/go

(of course the package names should match those that are actually in the source, otherwise it won't compile).

I looked at the go documentation, but it isn't clear to me whether this is doable.

答案1

得分: 3

你已经自己回答了这个问题,所以我只是确认这是你的做法。

在你的示例中,你可以使用:go get -u github.com/foo/bar/src/main/go

当然,这看起来不太美观,但请记住,大部分混乱来自于你自己控制的路径。

例如,它也可以是:go get -u github.com/foo/bar/go

我可以确认这个方法是可行的,因为这是我们在生产环境中使用的方法。

按设计,go get 的路径就是你从中获取它的地方。你还有另一个选择 - 使用重定向(例如 gopkg.in)。然而,这会增加额外的复杂性。

英文:

You've already answered the question yourself, so I'm just confirming that it is how you would do it.

In your example, you would use: go get -u github.com/foo/bar/src/main/go

Sure, it doesn't look pretty, but keep in mind that most of the yuckiness comes from paths in your control.

For example, it could be: go get -u github.com/foo/bar/go

I can confirm this works since this is what we use in production.

By design, the go get path is where you would get it from. You do have another option - to use redirection (like gopkg.in). However, that comes with its own added complexity.

huangapple
  • 本文由 发表于 2017年9月7日 06:32:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/46085245.html
匿名

发表评论

匿名网友

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

确定