简体   繁体   English

实践中的Golang工作区

[英]Golang Workspaces In Practice

According to the Go documentation they would like you to have a workspace that you should put all their projects in. 1 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. 根据Go文档,他们希望您拥有一个应将其所有项目放入其中的工作区。1但是,据我所知,一旦您要创建一个不使用Go的项目,这一切都会崩溃。只。

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. 该应用程序的只有一部分将用Go编写,并且该部分嵌套在该应用程序的子目录中。 How would I apply the Go workspace philosophy to this situation? 在这种情况下,我将如何应用Go工作区理念?


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

Using a different GOPATH per project is a very good and simple approach. 每个项目使用不同的GOPATH是一种非常好的简单方法。 In my experience this also works better than vendor since you can also install binaries and keep them on different versions. 以我的经验,这也比vendor更好,因为您还可以安装二进制文件并将它们保留在不同的版本上。

vg is a simple tool that helps managing workspaces, it integrates with your shell and detects automatically workspaces when you cd them. vg是一个简单的工具,可帮助管理工作区,它与您的shell集成在一起,并在您将它们cd时自动检测工作区。

Disclaimer: I am one of the authors of the tool. 免责声明:我是该工具的作者之一。

just set GOPATH according to your go files: 只需根据您的go文件设置GOPATH:

GOPATH=$PROJECT_PATH/app/posts GOPATH = $ PROJECT_PATH / app / posts

then put your source codes under 然后将您的源代码放在

$PROJECT_PATH/app/posts/src/package $ PROJECT_PATH / app / posts / src / package

You can put app/ in $GOPATH/src. 您可以将app /放在$ GOPATH / src中。 Then whenever you're ready to build, you specify the path of your source files, relative to where they are in GOPATH. 然后,无论何时准备构建,都可以指定源文件的路径,相对于它们在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. 如果您的应用程序源位于$GOPATH/src/app/而您的$GOPATH/src/app/posts/文件位于$GOPATH/src/app/posts/则可以使用go build $GOPATH/src/app/posts/posts.go源(让我们说app.posts /中的posts.go) go build $GOPATH/src/app/posts/posts.go或更好地使用app/作为当前工作目录来go build posts/posts.go

As of Go 1.11, go now has modules . 从Go 1.11开始,go现在具有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 . 除其他外,模块使您可以在$GOPATH之外具有隔离的源树(具有任意数量的软件包及其依赖项)。

You create a new module by running go mod init <module name> (you must be outside of $GOPATH/src to do this). 您可以通过运行go mod init <module name>创建一个新模块(您必须在$GOPATH/src才能执行此操作)。 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. 这将在当前文件夹中创建一个go.mod文件,并且您在该文件夹(或下面的任何文件夹)中运行的所有go命令都将使用该文件夹作为您的项目根目录。

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 您可以在这篇文章中阅读有关将go模块用作工作区的更多信息: https : //aliceh75.github.io/using-modules-for-workspaces-in-golang (免责声明:我已经写过),并且您可以阅读有关Go的更多信息。 Go Modules Wiki上的模块: https : //github.com/golang/go/wiki/Modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM