5
3

はじめに

一番初歩的な話ですが、海外の記事で話題になっていたのでまとめました。

結論

go run main.go より go run . を使いましょう。

なぜgo run .の方がいいのか?

go run main.goだと、以下のようにmainパッケージが2つに分かれているとコンパイルエラーになる可能性がある。あと、短い。

main.go
package main

func main() {
	hello()
}
hello.go
package main

import "fmt"

func hello() {
	fmt.Println("Hello!")
}
$ go run main.go
# command-line-arguments
./main.go:4:2: undefined: sayHello

go run .を使うと、正しく実行される。

$ go run .
Hello!

ファイルを指定する場合は、以下のようにする。

$ go run main.go hello.go
Hello!

元ネタ

Stop using go run main.go - YouTube

5
3
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
5
3