いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

Goメモ-16 (今の環境を崩さずにお手軽に別のバージョンのgoを使う)

概要

やり方をちょくちょく忘れてしまうので、忘れないうちにメモメモ。

今の環境は変えたくないけど、別のGoのバージョンを使いたいときに知ってると便利かもってやり方。

Dockerでコンテナ作ってしまうのもありだと思いますが、こっちもお手軽ですね。

こんな感じ

最近、Gitpod さんが気に入っててよく使っているのですが、GitpodさんのワークスペースにインストールされているGoが1.12でした。(2019-11-07現在)Chromebookでソース触りたいときとかに、とても便利です。

www.gitpod.io

で、自分のリポジトリのソースが go1.13 の機能を使っているので、ワークスペースを開くと以下のエラーが出ます。

gitpod /workspace/try-golang $ {
> test -f go.mod && go get -v ./...
> }; HISTFILE=/workspace/.gitpod/cmd-0 history -r
go: finding github.com/deckarep/golang-set v1.7.1
go: downloading github.com/deckarep/golang-set v1.7.1
go: extracting github.com/deckarep/golang-set v1.7.1
github.com/devlights/try-golang/books
github.com/devlights/try-golang/books/bootcamp
github.com/devlights/try-golang/books/concurrency
github.com/devlights/try-golang/effectivego
github.com/devlights/try-golang/basic/unittest
github.com/devlights/try-golang/advanced/closure
github.com/devlights/try-golang/advanced/reflection
github.com/devlights/try-golang/basic/array_
github.com/devlights/try-golang/basic/literals
github.com/devlights/try-golang/basic/comments
github.com/devlights/try-golang/basic/defer_
github.com/devlights/try-golang/basic/helloworld
github.com/devlights/try-golang/basic/import_
github.com/devlights/try-golang/basic/interface_
github.com/devlights/try-golang/basic/iota_
github.com/devlights/try-golang/basic/functions
github.com/devlights/try-golang/basic/error_
github.com/devlights/try-golang/basic/constants
github.com/devlights/try-golang/advanced/async
github.com/devlights/try-golang/basic/io_
github.com/deckarep/golang-set
# github.com/devlights/try-golang/basic/literals
basic/literals/binary_int_literals.go:9:11: syntax error: unexpected b1011, expecting semicolon or newline or )
note: module requires Go 1.13
github.com/devlights/try-golang/basic/map_
github.com/devlights/try-golang/basic/math_
github.com/devlights/try-golang/basic/os_
github.com/devlights/try-golang/basic/runtime_
github.com/devlights/try-golang/basic/scope/mypkg
github.com/devlights/try-golang/basic/slice_
github.com/devlights/try-golang/basic/stdin
github.com/devlights/try-golang/basic/stdout
github.com/devlights/try-golang/basic/string_
github.com/devlights/try-golang/basic/struct_
github.com/devlights/try-golang/basic/time_
github.com/devlights/try-golang/basic/type_
github.com/devlights/try-golang/basic/variables
github.com/devlights/try-golang/tutorial
github.com/devlights/try-golang/basic/scope
# github.com/devlights/try-golang/tutorial
tutorial/tutorial_gotour_25_reader.go:44:6: undefined: errors.Is
note: module requires Go 1.13
github.com/devlights/try-golang/advanced/sets

module requires Go 1.13 って出てますね。

見てみると

gitpod /workspace $ go version
go version go1.12 linux/amd64

確かに 1.12。

てことで、自分の環境内だけで 1.13.4 をインストールしてみます。

gitpod /workspace $ go get golang.org/dl/go1.13.4
gitpod /workspace $ go1.13.4 download
Downloaded   0.0% (    32768 / 120054682 bytes) ...
Downloaded   3.5% (  4227072 / 120054682 bytes) ...
Downloaded   7.0% (  8421376 / 120054682 bytes) ...
Downloaded  48.9% ( 58753024 / 120054682 bytes) ...
Downloaded 100.0% (120054682 / 120054682 bytes)
Unpacking /home/gitpod/sdk/go1.13.4/go1.13.4.linux-amd64.tar.gz ...
Success. You may now run 'go1.13.4'

無事 go get して、ダウンロード成功。「go1.13.4って叩くと起動するよ」って書かれているので

gitpod /workspace $ go1.13.4 version
go version go1.13.4 linux/amd64

確かに動いている。

てことで、自分のリポジトリをビルドしてみます。元々のgo (1.12) だと

gitpod /workspace/try-golang/cmd/trygolang $ go build
# github.com/devlights/try-golang/basic/literals
../../basic/literals/binary_int_literals.go:9:11: syntax error: unexpected b1011, expecting semicolon or newline or )
note: module requires Go 1.13
# github.com/devlights/try-golang/tutorial
../../tutorial/tutorial_gotour_25_reader.go:44:6: undefined: errors.Is
note: module requires Go 1.13

ってなりますが、

gitpod /workspace/try-golang/cmd/trygolang $ go1.13.4 build

だと、ちゃんとコンパイルできました。

コマンドの場所もこんな感じ。

gitpod ~ $ echo $GOROOT
/home/gitpod/go
gitpod ~ $ echo $GOPATH
/workspace/go
gitpod ~ $ which go
/home/gitpod/go/bin/go
gitpod ~ $ which go1.13.4 
/workspace/go/bin/go1.13.4

過去の記事については、以下のページからご参照下さい。

  • いろいろ備忘録日記まとめ

devlights.github.io

サンプルコードは、以下の場所で公開しています。

  • いろいろ備忘録日記サンプルソース置き場

github.com

github.com

github.com