LoginSignup
1
0

More than 1 year has passed since last update.

【Go】# command-line-arguments undefined: への対応

Posted at

はじめに

簡単な動作確認時にいつも忘れそうになるのでメモ。

やりたいこと

  1. 以下のようにsample-app内にmain.goとanother.goを作る。
  2. main関数内でanother関数を呼び出す。
  3. go run main.go コマンドを実行
sample-app
 └ main.go
 └ another.go
 └ go.mod
main.go
package main

import "fmt"

func main() {
	fmt.Println("main関数です。")
	another()
}
another.go
package main

import "fmt"

func another() {
	fmt.Println("another関数です。")
}

出力されるエラー

# command-line-arguments
./main.go:7:2: undefined: another

解消法

実行時のコマンドライン引数にanother.goも渡す
go run main.go another.go

またはカレントディレクトリ配下の全てのファイルを指定して実行する
go run .

実行結果

main関数です。
another関数です。
1
0
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
0