1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Go】エラー「go.mod file not found in current directory or any parent directory」が発生した場合の対処法

Posted at

概要

Go 言語でエラーが発生した場合の対処法の備忘録

事象

エラー: go: go.mod file not found in current directory or any parent directory
👉 Go 言語を初めて扱う場合によく遭遇するエラー
go_環境構築_09.png
(単純な Hello World 表示プログラムでのエラー例)

原因

go.mod ファイルが同階層もしくは親階層に見つからないため。

「go.mod ファイルが無いよ」と怒られているので、このファイルを用意する必要があります。
go.mod ファイルは、1つのプロジェクト(今回の事例なら「Hello World を表示するプログラム」という単位)で使用される go プログラム全体を管理するファイルのこと、…というイメージです。

対処法

ターミナルを開き、プロジェクトのディレクトリに移動したら、以下のコマンドを実行します(※ Hello World 表示プログラムでの例)

# go mod init {任意のプロジェクト名}
go mod init hello_world

すると、同階層に go.mod というファイルが生成され、以下の様な中身になっているはずです

module hello_world

go 1.19

ここまで出来たら、もう一度 hello_world.go ファイルを開き、何も変えずに保存処理をすると、エラーが解消されます。

本来なら、コーディングをする前に、事前にこの go.mod を用意した方が良いということですね。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?