为什么Golang包必须是v0或v1,而不能是v2020?

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

Why Golang packages have to be v0 or v1 and not v2020

问题

我到处都看到包必须有一个 v0 或 v1 的标签。为什么标签不能是 v2020 或其他不是 v0 或 v1 的值呢?我个人尝试过这样做,但是当我使用 v2020 时出现以下错误。

Scotts-Mac-mini:seeding syacko$ go mod tidy
go: errors parsing go.mod:
/Users/syacko/workspace/sotesoft/src/utils/seeding/go.mod:10: require gitlab.com/soteapps/packages: version "v2020.2.0" invalid: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2020
Scotts-Mac-mini:seeding syacko$

英文:

I read everywhere that packages have to have a tag of v0 or v1. Why can't a tag be v2020 or something other than v0 or v1. I have tried this personal and is get the following error with I use v2020.

Scotts-Mac-mini:seeding syacko$ go mod tidy
go: errors parsing go.mod:
/Users/syacko/workspace/sotesoft/src/utils/seeding/go.mod:10: require gitlab.com/soteapps/packages: version "v2020.2.0" invalid: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2020
Scotts-Mac-mini:seeding syacko$ 

答案1

得分: 7

这是一个约定,对所有人都很方便。Go模块选择使用广泛接受的语义化版本v2

Go模块维基页面:

> ### 如果我创建了go.mod文件但没有为我的存储库应用语义化版本标签会发生什么?
>
> 语义化版本是模块系统的基础。为了为消费者提供最佳体验,鼓励模块作者应用语义化版本的VCS标签(例如v0.1.0或v1.2.3-rc.1),但并不严格要求使用语义化版本的VCS标签:
>
> 1. 为了使go命令的行为符合文档,模块必须遵循语义化版本规范。这包括遵循语义化版本规范以确定何时允许进行重大更改。
>
> 2. 没有语义化版本的模块将由消费者使用伪版本的形式进行记录。通常情况下,这将是一个v0主版本,除非模块作者采用了“主目录”方法构建了一个v2+模块。
>
> 3. 因此,没有应用语义化版本的模块且没有创建“主目录”的模块实际上声明自己处于语义化版本v0的主版本系列中,模块的消费者将将其视为具有语义化版本v0的主版本。

Dave Cheney在Go模块之前发布的一篇有趣且相关的博客文章:Gophers,请为您的发布打上标签

> 我们想要什么?Go包的版本管理!我们什么时候想要?昨天!
>
> [...] 当您在项目中开始使用该包时,我们希望我们选择的Go构建工具能够获取最新的稳定版本。[...]
>
> 但是,截至目前,在2016年,无论是人还是工具,都无法查看一个“任意”的Git(或Mercurial、Bazaar等)Go代码存储库并询问以下问题:
>
> - 这个项目有哪些版本发布了?
> - 这个软件的最新稳定版本是什么?
> - 如果我有版本1.2.3,是否有应用了错误修复或安全更新的版本?
>
> 这是因为Go项目(Go包的存储库)没有版本,至少不是以其他语言中使用该词的方式。Go项目没有版本,因为没有形式化的发布流程。
>
> [...] 我建议Go项目采用语义化版本2.0.0。这是一个可靠的标准,不仅被Go程序员广泛理解,而且语义化版本将使人们能够编写工具,在最小的发布流程之上构建一个依赖管理生态系统。

英文:

It's a convention, a convenience for everyone. Go modules chose to use the widely accepted Semantic Versioning v2.

Go Modules Wiki:

> ### What happens if I create a go.mod but do not apply semver tags to my repository?
>
> semver is a foundation of the modules system. In order to provide the best experience for consumers, module authors are encouraged to apply semver VCS tags (e.g., v0.1.0 or v1.2.3-rc.1), but semver VCS tags are not strictly required:
>
> 1. Modules are required to follow the semver specification in order for the go command to behave as documented. This includes following the semver specification regarding how and when breaking changes are allowed.
>
> 2. Modules that do not have semver VCS tags are recorded by consumers using a semver version in the form of a pseudo-version. Typically this will be a v0 major version, unless the module author constructed a v2+ module following the "Major Subdirectory" approach.
>
> 3. Therefore, modules that do not apply semver VCS tags and have not created a "Major Subdirectory" are effectively declaring themselves to be in the semver v0 major version series, and a module-based consumer will treat them as having a semver v0 major version.

An interesting and relevant blog post from Dave Cheney that predates Go modules: Gophers, please tag your releases

> What do we want? Version management for Go packages! When do we want it? Yesterday!
>
> [...] We want our Go build tool of choice to fetch the latest stable version when you start using the package in your project.[...]
>
> But as it stands, today, in 2016, there is no way for a human, or a tool, to look at an arbitrary git (or mercurial, or bzr, etc) repository of Go code and ask questions like:
>
> - What versions of this project have been released?
> - What is the latest stable release of this software?
> - If I have version 1.2.3, is there a bugfix or security update that I should apply?
>
> The reason for this is Go projects (repositories of Go packages) do not have versions, at least not in the way that our friends in other languages use that word. Go projects do not have versions because there is no formalised release process.
>
> [...] I recommend that Go projects adopt SemVer 2.0.0. It’s a sound standard, it is well understood by many, not just Go programmers, and semantic versioning will let people write tools to build a dependency management ecosystem on top of a minimal release process.

答案2

得分: 4

在重新阅读github.com/golang/go/wiki/...之后,我发现问题不在于v2020.y.z,而是目录结构与版本号不匹配。路径abc/def/v2020 v2020.y.z应该可以工作。感谢提供的各种链接。一个好的工作示例是github.com/jackc/pgx/v4。

以下是您要进行版本更改的步骤:
当您想要将版本更改为V2或更高版本时,需要按照以下步骤进行操作。

版本更改前的示例go.mod文件

module gitlab.com/soteapps/packages

go 1.14

require (
	github.com/aws/aws-sdk-go v1.32.4
	github.com/jackc/pgconn v1.5.0
	github.com/jackc/pgx/v4 v4.6.0
	golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
	golang.org/x/text v0.3.3 // indirect
)

版本更改后的示例go.mod文件

module gitlab.com/soteapps/packages/v2100

go 1.14

require (
	github.com/aws/aws-sdk-go v1.32.4
	github.com/jackc/pgconn v1.5.0
	github.com/jackc/pgx/v4 v4.6.0
	golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
	golang.org/x/text v0.3.3 // indirect
)

版本更改前的示例Golang文件导入

package sDatabase

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/pgxpool"
	"gitlab.com/soteapps/packages/sError"
	"gitlab.com/soteapps/packages/sLogger"
)

const (

版本更改后的示例Golang文件导入

package sDatabase

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/pgxpool"
	"gitlab.com/soteapps/packages/v2020/sError"
	"gitlab.com/soteapps/packages/v2020/sLogger"
)

const (

具有v2+版本号的项目:

  1. 编辑go.mod文件中的模块行,将主版本号添加到现有模块路径的末尾。因此,上述模块行module gitlab.com/soteapps/packages将更改为module gitlab.com/soteapps/packages/v2100
  2. 在项目的所有*.go文件中,将对gitlab.com/soteapps/packages的所有导入引用更新为gitlab.com/soteapps/packages/{version}。在我们的示例中,这将是gitlab.com/soteapps/packages/v2100
  3. 将更改提交到源代码控制。
    • 如果您正在使用主分支,则提交到主分支。
      • 在提交到主分支后,从主分支创建一个带有版本名称的分支。在上面的示例中为v2100
    • 如果您正在使用分支,则提交到带有版本名称的分支。在上面的示例中为v2100
    • 最后一步是使用语义化版本格式(vX.Y.Z)创建一个指向版本分支的标签。在此示例中,v2100.1.0将是该标签。
英文:

After re-reading github.com/golang/go/wiki/…, I see the issue is not with the v2020.y.z, it is with the directory structure not matching the version number. path abc/def/v2020 v2020.y.z should work. Thank you for the various links. A good working example is github.com/jackc/pgx/v4

Here are the steps for the item you are going version:
When you get to the point where you want to change the version to V2 or higher, these are the steps that need to be followed.

Sample go.mod file before version change

module gitlab.com/soteapps/packages

go 1.14

require (
	github.com/aws/aws-sdk-go v1.32.4
	github.com/jackc/pgconn v1.5.0
	github.com/jackc/pgx/v4 v4.6.0
	golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
	golang.org/x/text v0.3.3 // indirect
)

Sample go.mod file after version change

module gitlab.com/soteapps/packages/v2100

go 1.14

require (
	github.com/aws/aws-sdk-go v1.32.4
	github.com/jackc/pgconn v1.5.0
	github.com/jackc/pgx/v4 v4.6.0
	golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
	golang.org/x/text v0.3.3 // indirect
)

Sample Golang file import before change

package sDatabase

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/pgxpool"
	"gitlab.com/soteapps/packages/sError"
	"gitlab.com/soteapps/packages/sLogger"
)

const (

Sample Golang file import after change

package sDatabase

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/pgxpool"
	"gitlab.com/soteapps/packages/v2020/sError"
	"gitlab.com/soteapps/packages/v2020/sLogger"
)

const (

Item having a version v2+ number:

  1. Edit the go.mod module line to include the major version number at the end of the existing module path. So, the above module line module gitlab.com/soteapps/packages would change too module gitlab.com/soteapps/packages/v2100.
  2. All import references to gitlab.com/soteapps/packages in all the *.go files in the project must be updated to gitlab.com/soteapps/packages/{version}. In our example, this would the following, gitlab.com/soteapps/packages/v2100.
  3. Commit the change to source control.
    1. If you are using master, then commit to master.
      1. After you commit to master, create a branch from master with the version name. v2100 in the above example.
    2. If you are using branch, then commit to a branch with the version name. v2100 in the above example.
    3. The last step is to create a tag using semantic versioning format (vX.Y.Z) that point to the version branch. v2100.1.0 would be the tag for this example.

huangapple
  • 本文由 发表于 2020年6月2日 04:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/62140832.html
匿名

发表评论

匿名网友

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

确定