はじめに
一番初歩的な話ですが、海外の記事で話題になっていたのでまとめました。
結論
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!