将多个程序转换为GO模块

发布于 2025-02-09 17:16:02 字数 1473 浏览 2 评论 0 原文

我们有一个较旧的GIT存储库,该存储库使用了较旧的Gopath结构。该存储库包括多个实用程序的代码,每个实用程序都有自己的Main.Go。此外,它包含一个带有多个子文件夹的“常见”文件夹,每个文件夹具有各种共享位功能。结果就是这样:

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)

在将其转换为GO模块时,我可以将所有这些都放在用于生产Muliple执行的一个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 Go Get Git git.acme.com /myrepo/common/颜色”,但这没有解决。我不会期望这两个都是必要的,因为所讨论的模块是此存储库的一部分。

我需要做什么才能使该进口工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情丝乱 2025-02-16 17:16:02

我可以将所有这些都放在用于生产Muliple执行的一个git回购中吗?

是的。

如果是,哪个子文件夹需要包含一个go.mod文件?

正如@Peter所指出的 - 只有您需要版本的版本。

只需 GO MOD INIT 如果您不确定 - 如果生成的途径不正确,则提供模块路径。


奖励建议,如果您正在版本使用该目录,请通过GIT标签标记您的单个目录。您可以为包(非墨包括)目录或可执行文件(主)软件包执行此操作。

如果需要,这将允许用户安装您的可执行文件的特定版本。

可执行文件的示例git标签:

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

用户可以安装类似的特定版本:

go install github.com/me/myrepo/[email protected]

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

如果包装需要您的依赖关系软件包的特定版本,请这样标记:

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

对于每个取决于这些软件的可执行文件,您将使用所需的版本执行 get get (依次更新 > go.mod ),

您可以从上面看到的标签匹配目录名称 - 然后以a

有关模块标签的信息,请参见官方博客文章发布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/[email protected]

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文