Brew is a package management tool on Mac, just like apt, yum, rpm on Linux, which can provide non-graphical software installation. Yesterday, while building the most powerful IDE in the universe, I used the brew tool to update the packages. Upgraded my Go version to the latest version, and wiped out the previously configured multiple versions of Go.

Option 1 brew switch

1. brew install

1
brew install go

By default you can install the latest version of go, and then install the specified version, using the brew switch command to switch between them.

2. brew switch

1
2
~ brew info go
go: stable 1.15.3 (bottled), HEAD

Using the brew info go command you can see the current versions of go that you can switch to, so you can install multiple versions and switch to the corresponding version.

1
2
3
4
// Install the specified go version
brew install go@<version>
// forexample
brew install go@1.12.17

Once installed, use brew info go to see if the switch is ready.

1
brew switch go 1.12.17

Using the above command alone you will find that go does not work anymore and the following message will appear.

1
2
3
4
~ brew switch go 1.12.17
Cleaning /usr/local/Cellar/go/1.12.17
Cleaning /usr/local/Cellar/go/1.15.3
0 links created for /usr/local/Cellar/go/1.12.17

Having created zero links means that it did not successfully point the go version under the version you need, what is the problem? Now cut the go version back to go 1.15.3 and you will find that you can switch and use it normally.

1
2
3
4
5
6
7
~ brew switch go 1.15.3
Cleaning /usr/local/Cellar/go/1.12.17
Cleaning /usr/local/Cellar/go/1.15.3
3 links created for /usr/local/Cellar/go/1.15.3

~ go version
go version go1.15.3 darwin/amd64

To locate this you need to look at why softlinks were not created for go version 1.12.17. The first step is to find where go is installed by default, use go env to see the installation directory.

1
/usr/local/Cellar/go/

Use the brew utility in the location of the MacOS Catalina system installation.

After entering the directory, there is only the default version 1.15.3 installed in the go directory, and there is no version installed by you, so you can exit the parent directory and see the downloaded version of go@1.12.17. Since the soft link is linked to the path above, you need to move this directory to the go directory.

1
2
3
4
5
6
7
8
// Open default directory
cd /usr/local/Cellar/go/
// Exit Directory
cd ..
// Move the directory to the go directory
mv go@1.12.17 go/
// IMPORTANT!!! Rename Folders
mv go@1.12.17 1.12.17

Next, use the switch command brew switch go <version> to switch environments.

Verify with Homebrew 3.2.9.

  1. Install the new version

    1
    
    brew install go@1.16 // Install go version 1.16
    
  2. Remove the original go version softlink

    1
    
    brew unlink go
    
  3. Specify a new version of the softlink

    1
    
    brew link go@1.16