概要
Goは複数のバージョンをインストールして遊ぶことがとても簡単な言語です。
基本、一つのバージョンのGoをインストールしてしまえば、後は Twitter の Goアカウントがよくつぶやいている以下の方法でいろんなバージョンを入れることが出来ます。
🎊 Go 1.17.6 and 1.16.13 are released!
— Go (@golang) 2022年1月6日
📢 Announcement: https://t.co/zjx4VAbi2K
📦 Download: https://t.co/BFIn7OWcFm#golang pic.twitter.com/vTLsFkv7u0
上記のとは別に、開発ブランチのGoを試すことができる gotip
というものも存在します。
こちらはリリースされていない機能なども含まれています。たとえば今現在(2022-01-27)とかでいうと go1.18 の機能も含まれています。
インストールは他のライブラリなどと同じで
$ go install golang.org/dl/gotip@latest
ってして
$ gotip download
ってやるだけです。gotip download
を実行するたびに最新版を取得してくれるので、一回入れた後はdownloadすれば最新版になっていきます。
使い方は go xxxx
ってしている部分を gotip xxxx
にするだけです。
$ gotip version go version devel go1.18-f4aa021 Thu Jan 27 00:03:31 2022 +0000 linux/amd64 $ gotip mod init app go: creating new go.mod: module app $ cat go.mod module app go 1.18
Cコンパイラが無い環境でCGO_ENABLED=1だとインストールに失敗する
gotipさんですが、CGO_ENABLED=1
の環境で、かつ、Cコンパイラ(gccなど。環境変数CCに値が設定されていない状態) が存在しない場合はエラーとなります。
$ gcc コマンド 'gcc' が見つかりません。次の方法でインストールできます: sudo apt install gcc $ go version go version go1.17.6 linux/amd64 $ go install golang.org/dl/gotip@latest go: downloading golang.org/dl v0.0.0-20220126164927-ab17da273865 $ gotip download Cloning into '/home/dev/sdk/gotip'... remote: Finding sources: 100% (11986/11986) remote: Total 11986 (delta 1449), reused 7421 (delta 1449) Receiving objects: 100% (11986/11986), 23.74 MiB | 10.04 MiB/s, done. Resolving deltas: 100% (1449/1449), done. Updating files: 100% (11159/11159), done. Updating the go development tree... From https://go.googlesource.com/go * branch master -> FETCH_HEAD HEAD is now at f4aa021 cmd/compile: support structural typing in unified IR Building Go cmd/dist using /home/linuxbrew/.linuxbrew/Cellar/go/1.17.6/libexec. (go1.17.6 linux/amd64) go tool dist: cannot invoke C compiler "gcc": exec: "gcc": executable file not found in $PATH Go needs a system C compiler for use with cgo. To set a C compiler, set CC=the-compiler. To disable cgo, set CGO_ENABLED=0. gotip: failed to build go: exit status 2
こんな感じになります。Cコンパイラが無い環境でインストールしたい場合は以下のようにしましょう。
$ env CGO_ENABLED=0 gotip download Updating the go development tree... From https://go.googlesource.com/go * branch master -> FETCH_HEAD HEAD is now at f4aa021 cmd/compile: support structural typing in unified IR Building Go cmd/dist using /home/linuxbrew/.linuxbrew/Cellar/go/1.17.6/libexec. (go1.17.6 linux/amd64) Building Go toolchain1 using /home/linuxbrew/.linuxbrew/Cellar/go/1.17.6/libexec. Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1. Building Go toolchain2 using go_bootstrap and Go toolchain1. Building Go toolchain3 using go_bootstrap and Go toolchain2. Building packages and commands for linux/amd64. --- Installed Go for linux/amd64 in /home/dev/sdk/gotip Installed commands in /home/dev/sdk/gotip/bin Success. You may now run 'gotip'! $ gotip version go version devel go1.18-f4aa021 Thu Jan 27 00:03:31 2022 +0000 linux/amd64
参考情報
- gotip command - golang.org/dl/gotip - pkg.go.dev
- Exploring Go v1.18's Generics - Big Nerd Ranch
- gotipでリリース前のGoを使って最先端Gopherになる
過去の記事については、以下のページからご参照下さい。
サンプルコードは、以下の場所で公開しています。