Project structure with v2 Go modules

Viewed 450

I have a Go project with structure somewhat like this

github.com/my/coolproject
│
└───project
    │   file1.go
    │   file2.go
    │   go.mod (github.com/my/coolproject/project)
    │   ...
    │
    └───package1
        │   file1.go
        │   file2.go
        │
        ...
        │  
        packagex
        │   file1.go (import github.com/my/coolproject/project/package1)
        │   file2.go

I would like to create v2 of the project but not sure how to go about doing that.

I tried creating v2 directory at the root of the repo, copy content of the project folder into it and update the go.mod file to github.com/my/coolproject/project/v2

With this I also had to update the imports to v2 however the directory structure now didn't match precisely so I had to change imports from

import github.com/my/coolproject/project/something to import github.com/my/coolproject/v2/something removing the 'project' and adding 'v2'.

This causes bunch of cannot find module ... errors, when I remove the 'project' from the go.mod module path in v2 then it starts working.

However now there is a difference between the original project import path and the v2

github.com/my/coolproject/project for the original vs v2 github.com/my/coolproject/v2

Is there a way how to keep the original with just v2 at the end? The problem is that then it doesn't really match the folder structure. I though about moving the v2 folder with new version into the project folder, but that sounds like a bad idea.

1 Answers

I though about moving the v2 folder with new version into the project folder, but that sounds like a bad idea.

Yet it seems to follow the official Go wiki on "Releasing Modules (v2 or Higher)"

Major subdirectory:

  • Create a new v2 subdirectory (e.g., my/module/v2) and place a new go.mod file in that subdirectory.
    The module path must end with /v2.
  • Copy or move the code into the v3 subdirectory.
  • Update import statements within the module to also use /v2 (e.g., import "github.com/my/module/v2/mypkg").
  • Tag the release with v2.0.0.

The other approach would be:

Major branch:

Update the go.mod file to include a /v3 at the end of the module path in the module directive (e.g., module github.com/my/module/v2).

  • Update import statements within the module to also use /v2 (e.g., import "github.com/my/module/v2/mypkg").
  • Tag the release with v2.0.0.

No subfolder to create there.
And you can set your default branch to a v2 branch instead of main, if you want people cloning your repository to get by default the latest version.

Related