title: [Learning Notes][Golang] "Version Retraction (Removal)" of Go 1.16 New Features (Go Modules retraction)
published: false
date: 2021-03-03 00:00:00 UTC
tags:
canonical_url: http://www.evanlin.com/til-go-retract/
---

## Preface:
Go Module officially introduced [Golang Modules](https://blog.golang.org/using-go-modules) in version 1.11, allowing many packages to use Go Module to manage dependencies. And in the Go 1.16 version, the Go Modules option is also enabled by default. However, the following problems may occur when developing a package:
- Suddenly发现某个的套件有重大的问题,希望其他人不要使用到这个套件。
- Accidentally incremented the version number too much, and some people have also used these versions. (e.g. originally wanted to run v0.4.0, but accidentally wrote v1.0.0)
The above two problems, if the package has not been distributed, are actually not a problem. But if the package is also distributed, it is necessary to use package retraction (Retract) to let the developers using the package understand the relevant problems, and also prevent future users from using this version number.
This article will introduce a feature in Go 1.16 that has not been heavily promoted (most people are paying attention to Apple M1 support), and also provide an implementation of version retraction through the official online example.
## TL;DR
This article will introduce:
-
[What is Retraction](#what-is-retraction)
-
[Why Retraction is Needed](#why-retraction)
-
[How to Cooperate to Retract Versions in the Past](#old-way-retraction)
-
[How to Use Go modules Retraction](#howto-retraction)
-
[Related Learning Resources](#retraction-reference)
## What is Retraction?
As the name suggests, it is the retraction of versions, that is, the "problematic" versions will be retracted.
## Why is Retraction Needed?
There are usually two types of problems:
- A critical error is found in a version that has been "published" and needs to be retracted.
- "Published" here means that it has been publicly released on github (or other repository) and is being used by someone.
- The version number was accidentally entered incorrectly, such as the 0.4.0 version, which was later accidentally entered as 1.0.0, but was then used by others.
## How to Cooperate to Retract Versions in the Past
Since Go Module Retraction was not provided in the past, if the above situation occurred, it could only be noted in the README. There was no way to obtain sufficient information at the same time in `go get`.
## How to Use Go modules Retraction
Here, through the online [Go Dev Playground](https://play-with-go.dev/retract-module-versions_go116_en/), we will directly explain the main problem-solving methods step by step. For detailed code, you can go [inside](https://play-with-go.dev/retract-module-versions_go116_en/) to view.
### Problem 1: What if a critical problem is found in a certain version?
Suppose you manage the package `gopher.live/ue0ddd4a99c02/proverb`, and the current version `0.2.0` has been released. But you found that this version has a major problem. You need to retract (or remove) this version, then you can enter the following command in the package's repo:
`go mod edit -retract=v0.2.0`
In this way, you will find that the `go.mod` file becomes the following content
module gopher.live/ue0ddd4a99c02/proverb
go 1.16
// Go proverb was totally wrong
retract v0.2.0
At this time, we can add some comments in the `go.mod` file, so that when others want to use it, relevant comments will also appear.
git add -A
$ git commit -q -m "Fix severe error in Go proverb"
$ git push -q origin main
remote: . Processing 1 references
remote: Processed 1 references in total
$ git tag v0.3.0
$ git push -q origin v0.3.0
Through the above method, you can advance the version number by one. And the correct content has been corrected.
If others want to pull down the problematic version, relevant warnings will appear.
go get gopher.live/ue0ddd4a99c02/proverb@v0.2.0
go: warning: gopher.live/ue0ddd4a99c02/proverb@v0.2.0: retracted by module author: Go proverb was totally wrong
go: to switch to the latest unretracted version, run:
go get gopher.live/ue0ddd4a99c02/proverb@latestgo get: downgraded gopher.live/ue0ddd4a99c02/proverb v0.3.0 => v0.2.0
In this way, you can use this method to achieve the process of retracting versions.
### Related Questions:
- If you don't execute `go get` to connect and query, can you not get the version retraction information?
- That's right, you still need to use `go get` or `go list` to get the data.
## Related Learning Resources
- [Using Go Modules](https://blog.golang.org/using-go-modules)
- [Go Dev Playground: Retract Module Versions](https://play-with-go.dev/retract-module-versions_go116_en/)
- [Retract Go Module Versions in Go 1.16](https://golangtutorial.dev/tips/retract-go-module-versions/)
Top comments (0)