Home > database >  Converting GOPATH repo with multiple programs into GO modules
Converting GOPATH repo with multiple programs into GO modules

Time:06-24

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?

CodePudding user response:

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

  • Related