DebianパッケージのGoバージョンを切り替える

手元のDebian sid環境にはDebinパッケージのいくつかのバージョンのGoが導入されています．

$ dpkg-query -W golang-1.??
golang-1.12 1.12.12-1
golang-1.15 1.15.15-1
golang-1.16 1.16.7-1
golang-1.17 1.17-2

現在は1.15に向いているようです．

$ go version
go version go1.15.15 linux/amd64
$ ls -l /usr/bin/go
lrwxrwxrwx 1 root root 21 Sep 17 2020 /usr/bin/go -> ../lib/go-1.15/bin/go

update-alternatives にあるかな?とみてみると見当たりません．

$ sudo update-alternatives --display go
update-alternatives: error: no alternatives for go
$ sudo update-alternatives --display go-lang
update-alternatives: error: no alternatives for go-lang
$ sudo update-alternatives --get-selections | grep -i go
bogofilter auto /usr/bin/bogofilter-bdb
fonts-japanese-gothic.ttf auto /usr/share/fonts/opentype/ipaexfont-gothic/ipaexg.ttf
google-chrome auto /usr/bin/google-chrome-stable
vendor-logos auto /usr/share/desktop-base/debian-logos

手動で update-alternatives に登録して，

$ sudo update-alternatives --install /usr/bin/go go /lib/go-1.12/bin/go 200 --slave /usr/bin/gofmt gofmt /lib/go-1.12/bin/gofmt
$ sudo update-alternatives --install /usr/bin/go go /lib/go-1.15/bin/go 200 --slave /usr/bin/gofmt gofmt /lib/go-1.15/bin/gofmt
$ sudo update-alternatives --install /usr/bin/go go /lib/go-1.16/bin/go 200 --slave /usr/bin/gofmt gofmt /lib/go-1.16/bin/gofmt
$ sudo update-alternatives --install /usr/bin/go go /lib/go-1.17/bin/go 200 --slave /usr/bin/gofmt gofmt /lib/go-1.17/bin/gofmt

Note

2021-09-03編集 ++
--slave /usr/bin/gofmt gofmt 〜 を追加して gofmt コマンドも追従するようにした．

今回は 1.16 にしてみます．

$ sudo update-alternatives --config go
There are 4 choices for the alternative go (providing /usr/bin/go).

Selection Path Priority Status
------------------------------------------------------------
0 /lib/go-1.12/bin/go 200 auto mode
* 1 /lib/go-1.12/bin/go 200 manual mode
2 /lib/go-1.15/bin/go 200 manual mode
3 /lib/go-1.16/bin/go 200 manual mode
4 /lib/go-1.17/bin/go 200 manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /lib/go-1.16/bin/go to provide /usr/bin/go (go) in manual mode
$ sudo update-alternatives --display go
go - manual mode
link best version is /lib/go-1.16/bin/go
link currently points to /lib/go-1.16/bin/go
link go is /usr/bin/go
/lib/go-1.12/bin/go - priority 200
/lib/go-1.15/bin/go - priority 200
/lib/go-1.16/bin/go - priority 200
/lib/go-1.17/bin/go - priority 200
$ go version
go version go1.16.7 linux/amd64

hello worldやGo 1.16 以降用のsourceもbuildできたのでとりあえず動いていそうです．

$ cat << __EOF__ > hello.go
> package main
>
> import "fmt"
>
>
func main() {
fmt.Println("hello world")
}
> __EOF__
$ go run ./hello.go
hello world

環境

$ dpkg-query -W golang-1.?? dpkg
dpkg 1.20.9
golang-1.12 1.12.12-1
golang-1.15 1.15.15-1
golang-1.16 1.16.7-1
golang-1.17 1.17-2
$ lsb_release -dr
Description: Debian GNU/Linux bookworm/sid
Release: unstable
$ arch
x86_64