在Google Go上的项目中,导入库的方式是使用`import`关键字。

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

Project on Google go, imports of libraries

问题

大家好。

我是Go语言的新手,目前正在努力理解构建Go应用程序的基础知识。我遇到了以下问题。

例如,我在我的项目中使用了其他库。我将它们保存在本地计算机上,所以我的项目可以正常工作。

我将我的代码上传到GitHub,另一个程序员下载了它。据我所知,我的代码将无法工作,因为这个程序员没有我使用的库。

所以问题是:分享我的项目及其所有库的最佳方法是什么?我应该将这些库上传到单独的存储库中吗?然后,为了使用我的项目,人们需要查看代码以检测我使用了哪些库,并逐个下载它们吗?

例如,在Java中有像Maven或Ant这样的工具,可以下载所有所需的依赖项。Go语言中是否有类似的工具?

让我们将我的项目的主文件命名为main.go,我正在使用自己的库:mathutil.go。

在其他计算机上使该项目运行的最佳方法是什么?

英文:

everyone.

I am new to Go language and currently I am trying to understand the basics of building Go applications. I met the following problem.

For example, I am using other libraries in my project. I have them locally, on my computer, so my project works fine.

I am loading my code on github and another programmer download it. As I understand, my code won't work, because this programmer doesn't have the libraries I used.

So the question is: What is the best way to share my project with all libraries it has? Should I upload these libraries in the separate repositories? Then to use my project, people need to look inside the code to detect which libraries I am using to download them one by one?

For example, in Java there is such thing like Maven or Ant, which downloads all required dependencies. Is there any tools like this for Go?

Let's call the main file of my project main.go
And I am using my own library: mathutil.go

what is the best way to make this project run on other computers?

答案1

得分: 3

Go的依赖关系工作方式与使用Maven或IVY的传递依赖关系非常相似。当有人使用"go get"命令获取你的包时,你所依赖的任何内容都会自动下载。

例如,在你的源代码中:

import "github.com/foo/bar"

go会自动将其下载到$GOPATH/src/github.com/foo/bar目录下,同时也会下载你的代码。

假设你使用的第三方库托管在公共仓库(例如:github)上,那么其他人就不需要做任何操作。

如果你使用的库在公共仓库上不可用,你需要将它们发布到某个地方,前提是它们的许可证允许这样做。

请查看golang.org/doc/code.html获取更多详细信息。

英文:

Go's dependencies work very much like using Maven or IVY transitive dependencies. When someone does "go get" of your package, anything you depend on will automatically download.

For example, in your source:

import "github.com/foo/bar"

go will automatically download that to your $GOPATH/src/github.com/foo/bar along with your code.

Assuming the third party libs you use are hosted in a public repo (ie: github) then people don't need to do anything.

If the libraries you used are not available on a public repo, you will need to post them somewhere assuming their licensing allows.

Take a look at golang.org/doc/code.html for more details

huangapple
  • 本文由 发表于 2014年5月18日 23:37:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/23723707.html
匿名

发表评论

匿名网友

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

确定