スマレジエンジニアyushiのブログ

スマレジエンジニアのブログ

【TODOリスト 第10回】Golangのバージョン更新

久しぶりにTODOリストを作っていきます。

yushi-dev.hatenablog.com

今回は、Golangのバージョンを更新します。

Golangのマイナーバージョン固定

久しぶりにプロジェクトをビルドしようとしたら、エラーが出てしまいました。

docker compose build --pull

=> ERROR [3/3] RUN go get -u github.com/go-sql-driver/mysql   gith  0.3s
------
 > [3/3] RUN go get -u github.com/go-sql-driver/mysql   github.com/gin-gonic/gin   github.com/gin-contrib/cors:
#6 0.332 go: go.mod file not found in current directory or any parent directory.
#6 0.332        'go get' is no longer supported outside a module.
#6 0.332        To build and install a command, use 'go install' with a version,
#6 0.332        like 'go install example.com/cmd@latest'
#6 0.332        For more information, see https://golang.org/doc/go-get-install-deprecation
#6 0.332        or run 'go help get' or 'go help install'.
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c go get -u github.com/go-sql-driver/mysql   github.com/gin-gonic/gin   github.com/gin-contrib/cors]: exit code: 1

golangの1.18より、go getが外部モジュール導入に利用できなくなったことが原因のようです。

そもそも、golangではマイナーバージョン更新で仕様が変わっていっているため、マイナーバージョンを固定してあげる必要があるようです。

- FROM golang:1-alpine
+ FROM golang:1.16-alpine

これで無事ビルドが通るようになります。

Golangのバージョンを最新化

改めて、バージョンを最新の1.18に更新してみます。

- FROM golang:1.16-alpine
+ FROM golang:1.18-alpine

もちろん、前項のエラーが発生してしましますので、修正します。

- FROM golang:1.16-alpine

- RUN apk add --no-cache git

- RUN go get -u github.com/go-sql-driver/mysql \
-   github.com/gin-gonic/gin \
-   github.com/gin-contrib/cors
+ FROM golang:1.18-alpine

Dockerfileで外部モジュールを導入しようとしていますが、これが間違いでした。
go.modの記述によって、golangのビルド時に自動でモジュール導入してくれるようです。

これを削除することで、無事エラーを解決できました。

go.mod, go.sumの更新

最後に、go.mod, go.sumのバージョン更新を行います。

go mod edit -go=1.18
go mod tidy

Pull Request

https://github.com/nek0meshi/todo-list/pull/17

まとめ

久しぶりにGolangを触ってみました。
go.mod等の仕様を全然把握できておらず、少しずつ勉強していきたいです。