项目结构与v2 Go模块

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

Project structure with v2 Go modules

问题

我有一个类似这样的Go项目结构:

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

我想创建项目的v2版本,但不确定如何操作。

我尝试在仓库的根目录下创建v2目录,将project文件夹的内容复制到其中,并更新go.mod文件为github.com/my/coolproject/project/v2

这样做后,我还必须更新导入语句为v2版本,但是目录结构不完全匹配,所以我不得不将导入语句从

import github.com/my/coolproject/project/something 改为 import github.com/my/coolproject/v2/something,去掉了'project'并添加了'v2'。

这导致了一系列的"无法找到模块..."错误,当我从v2的go.mod模块路径中移除'project'时,它开始工作。

然而,现在原始项目的导入路径和v2版本有所不同:

github.com/my/coolproject/project 对应原始版本,而v2版本为 github.com/my/coolproject/v2

有没有办法保持原始版本的路径,只在末尾添加v2?问题在于这样做实际上与文件夹结构不匹配。我考虑过将新版本的v2文件夹移动到project文件夹中,但这听起来像个不好的主意。

英文:

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

得分: 3

我考虑将带有新版本的v2文件夹移动到项目文件夹中,但这听起来不是一个好主意。

然而,根据官方的Go Wiki上的“发布模块(v2或更高版本)”的说明,似乎是可以这样做的。

主要的子目录方法如下:

  • 创建一个新的v2子目录(例如,my/module/v2)并在该子目录中放置一个新的go.mod文件。模块路径必须以/v2结尾。
  • 将代码复制或移动到v3子目录中。
  • 在模块内部的导入语句中也使用/v2(例如,import "github.com/my/module/v2/mypkg")。
  • 使用v2.0.0标记发布。

另一种方法是:

主要的分支方法如下:

  • 更新go.mod文件,在模块指令的模块路径末尾添加/v3(例如,module github.com/my/module/v2)。
  • 在模块内部的导入语句中也使用/v2(例如,import "github.com/my/module/v2/mypkg")。
  • 使用v2.0.0标记发布。

这种方法不需要创建子文件夹。如果你希望克隆你的存储库的人默认获取最新版本,你可以将默认分支设置为v2分支。

英文:

> 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.

huangapple
  • 本文由 发表于 2021年11月22日 06:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/70058951.html
匿名

发表评论

匿名网友

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

确定