伪版本是Go用于标识未打tag提交的字符串,格式为v0.0.0-YYYYMMDDHHMMSS-hash前缀,由工具链自动基于author time和commit hash生成,不可手动构造或干预。

什么是伪版本(pseudo-version)?
Go 的伪版本是形如 v0.0.0-20190711162855-6b37a7e2088a 的字符串,由三部分组成:基础版本号 + 提交时间戳 + 提交哈希前缀。它用于标识一个尚未打正式 tag 的 commit,常见于 go get 直接拉取某分支或某次提交时。
go list -m -json 能否直接拿到伪版本?
不能直接生成,但能验证或反查——当模块已出现在 go.mod 中且使用了伪版本,go list -m -json <module></module> 会返回 Version 字段值(即该伪版本)。但它不接受时间戳输入,也不参与生成逻辑。
- 伪版本的生成完全由 Go 工具链内部完成,用户不可干预格式或算法
- 你提供的是 commit hash 或 branch 名,Go 自动解析其对应 commit 的
author time(非 committer time),再格式化为YYYYMMDDHHMMSS - 若 commit 时间为 2024-03-15 09:22:33 +0800,则时间戳部分为
20240315092233
手动构造伪版本前,先确认 commit 是否可被识别
Go 要求该 commit 必须存在于模块的源码仓库中,且能通过 git ls-remote 或本地 clone 找到。否则 go get 会报错:invalid version: unknown revision。
使用 uber-go/dig 在 Golang 中实现依赖注入——基于反射容器,提供 Provide/Invoke、dig.In/dig.Out 参数与结果对象,支持命名依赖等
- 用
git show -s --format='format:%at %h' <commit></commit>查看 Unix 时间戳和 hash,确认 commit 存在 - 时间戳必须是 author time(
%at),不是%ct;Go 工具链只读 author 时间 - hash 取前 12 位小写(
git rev-parse --short=12 <commit></commit>),不足则全取 - 基础版本号通常为
v0.0.0,除非模块有go.mod里声明的module名且存在更早的 semver tag
真正起作用的是 go get 的 commit 引用方式
你不需要手拼伪版本字符串。Go 会在执行 go get 时自动计算并写入 go.mod —— 只要你给的是可解析的 commit 标识符。
立即学习“go语言免费学习笔记(深入)”;
-
go get example.com/repo@abcd1234→ 自动生成v0.0.0-20240315092233-abcd1234... -
go get example.com/repo@main→ 先解析main到最新 commit,再按其 author time 生成 -
go get example.com/repo@v0.0.0-00010101000000-000000000000是非法的,Go 不接受伪造的时间戳 - 如果本地
replace指向本地路径,go mod tidy不会生成伪版本,也不会读 commit 时间
时间戳本身只是伪版本的“可读锚点”,真正校验依赖一致性的仍是 commit hash。别为了对齐时间戳去改 author date——那会破坏 Git 历史一致性。

















