Golang工作区实践

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

Golang Workspaces In Practice

问题

根据Go文档,他们希望你有一个工作空间,你应该把所有的项目都放在里面。然而,据我所知,一旦你想要创建一个不仅仅使用Go的项目,这一切就都不成立了。

以一个由多个微服务组成的项目为例。假设它的结构如下:

app/
    authentication/ (使用Rust)
    users/ (使用NodeJS)
    posts/ (使用Go)

只有应用的一部分是用Go编写的,而且这部分嵌套在应用的子目录中。在这种情况下,我该如何应用Go工作空间的理念呢?


  1. https://golang.org/doc/code.html#Workspaces
英文:

According to the Go documentation they would like you to have a workspace that you should put all their projects in.<sup>1</sup> However, as far as I can tell, this all falls apart as soon as you want to make a project that does not use Go exclusively.

Take a project where it is made up of many micoservices for example. Lets say that it is structured like this:

app/
    authentication/ (Using rust)
    users/ (Using NodeJS)
    posts/ (Using Go)

Only one part of the app would be written in Go, and that part is nested in a subdirectory of the app. How would I apply the Go workspace philosophy to this situation?


  1. https://golang.org/doc/code.html#Workspaces

答案1

得分: 7

使用每个项目不同的GOPATH是一个非常好且简单的方法。根据我的经验,这种方法比vendor更好用,因为你还可以安装二进制文件并保持它们的不同版本。

vg是一个简单的工具,可以帮助管理工作空间,它与你的shell集成,在你cd到工作空间时会自动检测到。

免责声明:我是该工具的作者之一。

英文:

Using a different GOPATH per project is a very good and simple approach. In my experience this also works better than vendor since you can also install binaries and keep them on different versions.

vg is a simple tool that helps managing workspaces, it integrates with your shell and detects automatically workspaces when you cd them.

Disclaimer: I am one of the authors of the tool.

答案2

得分: 3

从Go 1.11版本开始,Go现在支持模块。除其他功能外,模块还可以让你在$GOPATH之外创建具有独立源代码树的项目(包括任意数量的包和它们自己的依赖)。

你可以通过运行go mod init <模块名>(必须在$GOPATH/src之外)来创建一个新的模块。这将在当前文件夹中创建一个go.mod文件,并且你在该文件夹中(或其子文件夹中)运行的任何go命令都将使用该文件夹作为项目根目录。

你可以在这篇文章中了解更多关于使用Go模块作为工作区的内容:https://aliceh75.github.io/using-modules-for-workspaces-in-golang(免责声明:我写的),你也可以在Go模块维基页面上了解更多关于Go模块的信息:
https://github.com/golang/go/wiki/Modules

英文:

As of Go 1.11, go now has modules. Amongst other things, modules enable you to have isolated source trees (with any number of packages and their own dependencies) outside of your $GOPATH.

You create a new module by running go mod init &lt;module name&gt; (you must be outside of $GOPATH/src to do this). This will create a go.mod file in the current folder, and any go command you run in that folder (or any folder beneath) will use that folder as your project root.

You can read more about using go modules as workspaces in this post: https://aliceh75.github.io/using-modules-for-workspaces-in-golang (disclaimer: I wrote it), and you can read more about Go modules on the Go Modules Wiki:
https://github.com/golang/go/wiki/Modules

答案3

得分: 2

根据你的Go文件设置GOPATH:

GOPATH=$PROJECT_PATH/app/posts

然后将你的源代码放在以下路径下:

$PROJECT_PATH/app/posts/src/package

英文:

just set GOPATH according to your go files:

> GOPATH=$PROJECT_PATH/app/posts

then put your source codes under

> $PROJECT_PATH/app/posts/src/package

答案4

得分: 2

你可以将 app/ 放在 $GOPATH/src 目录下。然后,每当你准备构建时,你可以指定源文件的路径,相对于它们在 GOPATH 中的位置。

例如:

如果你的应用源代码在 $GOPATH/src/app/ 中,而你的 .go 文件在 $GOPATH/src/app/posts/ 中,那么你可以使用 go build $GOPATH/src/app/posts/posts.go 或者更好的方式 go build posts/posts.go 来构建一个源文件(假设是 app/posts/posts.go),并将 app/ 设置为当前工作目录。

英文:

You can put app/ in $GOPATH/src. Then whenever you're ready to build, you specify the path of your source files, relative to where they are in GOPATH.

For example:

if your app source is in $GOPATH/src/app/ and your .go files are in $GOPATH/src/app/posts/ then you can build a source (lets say posts.go in app/posts/) with go build $GOPATH/src/app/posts/posts.go or better go build posts/posts.go with app/ as your current working directory.

huangapple
  • 本文由 发表于 2016年3月17日 07:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/36049030.html
匿名

发表评论

匿名网友

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

确定