𝗣𝗮𝗰𝗸𝗮𝗴𝗲.𝗷𝘀𝗼𝗻 𝘃𝘀 𝗴𝗼.𝗺𝗼𝗱: 𝗪𝗵𝗲𝗿𝗲 𝗗𝗶𝗱 𝘁𝗵𝗲 𝗩𝗲𝗿𝘀𝗶𝗼𝗻 𝗙𝗶𝗲𝗹𝗱 𝗚𝗼?
If you move from JavaScript to Go, one thing will surprise you.
Open a package.json file. You will see a version field right at the top. It is easy to read. You can change it in a pull request. It lives inside your code.
Now open a go.mod file.
The version is not there. This is not a mistake. It is a choice.
Go does not use a version field for your own module. It uses git tags instead.
How it works: • You run git tag v1.2.3 • You push the tag to your repository • That tag becomes your version
When someone runs go get, Go looks at your git tags to find the right commit. The tag is the single source of truth.
This design has a major strength. A version can never point to the wrong code. In npm, the published code and the version field can drift apart. In Go, they are the same thing. The version is the commit.
However, this changes your workflow:
- Visibility: You cannot see your version by looking at a file. You must run a git command to find it.
- Code Reviews: A version bump does not show up in a code diff. It is a tag push, not a code change.
- Local Builds: A commit without a tag shows a messy pseudo-version. You only get a clean version after you tag the commit.
- Major Versions: This is the biggest change. In Go, v2 and above must include the version in the import path.
Example: v1 uses github.com/you/my-app v2 uses github.com/you/my-app/v2
This allows one program to use two different major versions of the same library without conflict.
Most other languages keep the version in a file: • Node: package.json • Rust: Cargo.toml • Python: pyproject.toml • Java: pom.xml
Go is the outlier. It relies strictly on git tags.
If you miss the npm experience, you can inject the version into your binary at build time using ldflags. This allows your app to respond to a version command.
The trade-off is simple: A version field is easy to read and review, but it can lie. A git tag is hard to see, but it is always true.
Go chose truth over convenience.
To the Go developers: Is this the best model? Would you prefer a version field in go.mod if the tools verified it against the git tag?
Source: https://dev.to/dalirnet/packagejson-vs-gomod-where-did-the-version-field-go-3301