将具有多个程序的GOPATH存储库转换为GO模块。

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

Converting GOPATH repo with multiple programs into GO modules

问题

我们有一个较旧的git存储库,它使用较旧的GOPATH结构。该存储库包含多个实用程序的代码,每个实用程序都有自己的main.go。此外,它还包含一个名为"common"的文件夹,其中包含多个子文件夹,每个子文件夹都有各种共享功能。结果类似于这样:

progOne\main.go(以及其他代码,一些在子文件夹中)
progTwo\main.go(以及其他代码,一些在子文件夹中)
progThree\main.go(以及其他代码,一些在子文件夹中)
common\net\(go文件,但没有main)
common\colors\(go文件,但没有main)
common\resources\(go文件,但没有main)

在将其转换为go模块时,我可以将所有内容保留在一个git存储库中,用于生成多个可执行文件吗?如果可以,哪些子文件夹需要包含go.mod文件?

编辑:感谢@colm.anseo的详细回答。这可能是一个单独的问题,但由于它与之前的问题密切相关,我将在这里提问。

一旦我按照建议在存储库的根目录中创建了go.mod文件,我在VSCode中打开了根文件夹。我想调试"ProgOne",所以我设置了一个类似于这样的启动配置:

{
   "name": "Launch Package",
   "type": "go",
   "request": "launch",
   "mode": "debug",
   "program": "${workspaceFolder}/progOne/main.go",
   "buildFlags": ""
}

但是当我尝试启动调试器时,构建失败了:

Starting: /Users/me/dev/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:58867 --log-dest=3 from /Users/me/dev/convoy/myRepo/progOne
DAP server listening at: 127.0.0.1:58867
Build Error: go build -o /Users/me/dev/convoy/myRepo/progOne/__debug_bin -gcflags all=-N -l ./main.go
main.go:20:2: no required module provides package git.acme.com/myRepo/common/colors; to add it:
	go get git.acme.com/myRepo/common/colors
(exit status 1)

我运行了"go mod tidy"和"go get git.acme.com/myRepo/common/colors",但没有解决问题。我不希望这两个命令都是必需的,因为所涉及的模块是该存储库的一部分。

我需要做什么才能使该导入工作?

英文:

We have an older git repository that was uses the older GOPATH structure. That repository includes code for multiple utilities, each with its own main.go. Additionally, it contains a "common" folder with multiple subfolders, each with various shared bits functionality. The result is something like this:

progOne\main.go (and other code, some in subfolders)
progTwo\main.go (and other code, some in subfolders)
progThree\main.go (and other code, some in subfolders)
common\net\ (go files, but no main)
common\colors\ (go files, but no main)
common\resources\ (go files, but no main)

In converting this to go modules, can I leave all of this in one git repo that is used to produce muliple executibles? If yes, which of the subfolders need to include a go.mod file?

EDIT: Thanks to @colm.anseo for his thorough response. This is probably a separate question, but since it is closely related, I'll ask it here.

Once I created a go.mod file in the root of the repo as suggested, I opened the root folder in VSCode. I'd like to debug "ProgOne", so I set up a launch configuration like this:

{
   "name": "Launch Package",
   "type": "go",
   "request": "launch",
   "mode": "debug",
   "program": "${workspaceFolder}/progOne/main.go",
   "buildFlags": ""
}

But when I attempt to launch the debugger, the build fails:

Starting: /Users/me/dev/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:58867 --log-dest=3 from /Users/me/dev/convoy/myRepo/progOne
DAP server listening at: 127.0.0.1:58867
Build Error: go build -o /Users/me/dev/convoy/myRepo/progOne/__debug_bin -gcflags all=-N -l ./main.go
main.go:20:2: no required module provides package git.acme.com/myRepo/common/colors; to add it:
	go get git.acme.com/myRepo/common/colors
(exit status 1)

I ran both "go mod tidy" and "go get git.acme.com/myRepo/common/colors", but that didn't fix it. I wouldn't expect either of those to be necessary, since the module in question is part of this repository.

What do I need to do to get that import working?

答案1

得分: 1

> 我可以将所有内容放在一个git仓库中,用于生成多个可执行文件吗?

可以。

> 如果可以,哪些子文件夹需要包含go.mod文件?

正如@Peter所指出的,只需要包含需要版本控制的子文件夹。

如果不确定,只需运行go mod init命令,如果生成的模块路径不正确,可以提供一个正确的模块路径。


额外建议,如果你对某个目录进行版本控制,可以通过git标签对其进行标记。你可以对包(非main包)目录或可执行文件(main包)进行标记。

这样,用户可以根据需要安装特定版本的可执行文件。

例如,对于可执行文件的git标签:

progOne/v0.1.0
progTwo/v0.2.5
progThree/v0.3.4

用户可以按以下方式安装特定版本:

go install github.com/me/myrepo/progOne@v0.1.0

或者使用@latest来获取最新版本。

如果包需要特定版本的依赖包,可以这样对它们进行标记:

common/net/v0.1.2
common/colors/v0.3.5
common/resources/v0.5.9

对于每个依赖这些包的可执行文件,可以使用所需的版本执行go get命令(这将更新go.mod文件)。

如上所示,标签应与目录名匹配,并以SemVer结尾。

有关模块标记的详细信息,请参阅官方博文发布Go模块

英文:

> can I leave all of this in one git repo that is used to produce muliple executibles?

Yes.

> If yes, which of the subfolders need to include a go.mod file?

As @Peter noted - only the ones you need to version.

Just go mod init if you are unsure - and provide a module path if the one that is generated is not correct.


Bonus suggestion, tag your individual directories via git tags if you are versioning that directory. You can do this for packages (non-main) directories or executable (main) packages.

This will allow user to install specific versions of your executables if need be.

Example git tags for the executable(s):

progOne/v0.1.0
progTwo/v0.2.5
progThree/v0.3.4

and a user could install a specific version like so:

go install github.com/me/myrepo/progOne@v0.1.0

or use @latest to grab the latest version.

And if the packages need specific version of your dependency packages, tag them like so:

common/net/v0.1.2
common/colors/v0.3.5
common/resources/v0.5.9

for each executable that depends on these, you would perform a go get with the desired version (which will in turn update go.mod)

As you can see from above the tag should match the directory name - and then end with a SemVer.

For information on module tagging see the official blog post Publishing Go Modules

huangapple
  • 本文由 发表于 2022年6月23日 05:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/72722419.html
匿名

发表评论

匿名网友

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

确定