いろいろ備忘録日記

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

go.mod ファイルのGoランタイムのバージョン変更の仕方 (go mod edit)

概要

忘れないうちにメモメモ。

Go Modules を使って、モジュール管理するのが今後のやり方になると思いますが

一旦、作成した go.mod ファイルの

go 1.12

とか記載されるランタイムのバージョンってどうやって更新するのかなってなって、ちょっと調べたのでメモ。

環境

$ sw_vers 
ProductName:    Mac OS X
ProductVersion: 10.14.5
$ go version
go version go1.12.8 darwin/amd64

go.mod ファイルって手でいじったら駄目な感じがする・・・

go.mod ファイル自体が、

go mod init name/of/package

って感じで生成して、それ以降も自動的に調整されていくので

手でいじったら駄目な感じがしてました。

ちゃんと edit ってサブコマンドがあった

わからんかったら、素直にヘルプ見るですね

 go mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

    go mod <command> [arguments]

The commands are:

    download    download modules to local cache
    edit        edit go.mod from tools or scripts
    graph       print module requirement graph
    init        initialize new module in current directory
    tidy        add missing and remove unused modules
    vendor      make vendored copy of dependencies
    verify      verify dependencies have expected content
    why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.

ちゃんと edit ってサブコマンドありました。これ使えばいいのだろうと予想。

$ go mod edit
go mod edit: no flags specified (see 'go help mod edit').

フラグが指定されていないってメッセージでた。。指定が足りていないみたい。なので、書かれているヘルプみる。

$ go help mod edit
usage: go mod edit [editing flags] [go.mod]

Edit provides a command-line interface for editing go.mod,
for use primarily by tools or scripts. It reads only go.mod;
it does not look up information about the modules involved.
By default, edit reads and writes the go.mod file of the main module,
but a different target file can be specified after the editing flags.

・・省略・・・

めっちゃ大量にヘルプでた。。もうちょい絞りたいな・・・

$ go help mod edit | grep version
The -require=path@version and -droprequire=path flags
add and drop a requirement on the given module path and version.
Users should prefer 'go get path@version' or 'go get path@none',
The -exclude=path@version and -dropexclude=path@version flags
add and drop an exclusion for the given module path and version.
Note that -exclude=path@version is a no-op if that exclusion already exists.
add and drop a replacement of the given module path and version pair.
If the @v in old@v is omitted, the replacement applies to all versions
The -go=version flag sets the expected Go language version.

お、一番下に -go=version ってフラグを指定したらセットされるって記載がある。

てことで、試してみます。(下にでてくる try-golang ってのは自分用の勉強リポジトリです)

$ go version
go version go1.12.8 darwin/amd64


# 今の go.mod (go 1.12ってなってる)
$ cat go.mod
module github.com/devlights/try-golang

go 1.12

require github.com/deckarep/golang-set v1.7.1

# 変更してみる (1.12 --> 1.11)
$ go mod edit -go=1.11

# 差分見てみる
$ git diff
diff --git a/go.mod b/go.mod
index 5d2648c..7891f6d 100644
--- a/go.mod
+++ b/go.mod
@@ -1,5 +1,5 @@
 module github.com/devlights/try-golang

-go 1.12
+go 1.11

 require github.com/deckarep/golang-set v1.7.1

# 元に戻す
$ git checkout -- go.mod

ちゃんと変わりますね。これで次のメジャーバージョンアップとかしたときに

go mod edit -go=1.13

とかやったら、ちゃんと変更できそう。


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

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

devlights.github.io

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

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

github.com

github.com

github.com