タイトル: go: go.mod file not found in current directory or any parent directory.
SEOタイトル: 【Go言語エラー】 go.mod file not found in current directory or any parent directory.
エラー内容
| >go get -u google.golang.org/grpc go: go.mod file not found in current directory or any parent directory. 'go get' is no longer supported outside a module. To build and install a command, use 'go install' with a version, like 'go install example.com/cmd@latest' For more information, see https://golang.org/doc/go-get-install-deprecation or run 'go help get' or 'go help install'. |
発生条件と原因
このエラーメッセージは、Goモジュールが初期化されていないために発生しています。GoモジュールはGo 1.11以降で導入されたもので、go getが使用されなくなりました。
対処法
Goモジュールの初期化:
プロジェクトのルートディレクトリで、以下のコマンドを実行してGoモジュールを初期化します。
| go mod init my-web-app |
これにより、go.mod ファイルが生成されます。
依存関係の取得:
依存関係の取得には、go getではなく、go installを使用します。
| go install google.golang.org/grpc@latest go install github.com/golang/protobuf/protoc-gen-go@latest |
これにより、依存関係が go.mod と go.sum ファイルに記録されます。
その後、先程の手順を続けてください。例えば、Docker、Go、Protocol Buffersのインストールやアプリケーションのビルドなどを行ってください。