package main
import "fmt"
func man() {
fmt.Println("Hello World")
}
해당 프로그램은 "Hello World"를 항상 프린트하는 executable file을 만든다. 프로그램을 다시 run하고 싶을 때 프로그램을 compile할 필이 없이 그냥 executable file을 run하면 된다. 즉 사용자와 상호작용하는 빠른 코드를 원한다면 프로그램을 한번 compile하고 executable file을 쓰면 된다.
프로그램을 수정하고 싶으면 경우에는 어떻게 할까? go build로 다시 다른 executable file을 compile하고 run하는 것은 비효율적이다. 작은 에러 하나 발견할 때마다 이렇게 진행하는 것은 대단히~ 비효율적이다. 이 때 go run을 쓰면 된다. go run은 compile + execution을 둘 다 처리해주기 때문이다. 새롭게 고친 코드의 아웃풋을 확인할 때 매우 유용하다.
go run은 executable file을 현재 폴더에 생성 안된다.
compile된 binary를 특정 경로에 아웃풋 되게끔 해준다.
go build와 똑같은 작업을 하지만 binary를 $GOPATH/bin 디렉토리에, go get을 통해 설치했던 tool들 사이에 추가한다. 이제 $GOPATH/bin/hello을 하면 터미널에 Hello, world가 뜰 것이다.
Compile하는 이유는?
Go 코드를 컴퓨터 언어 -> binary code로 변환시키기 위해!
executable file 생성 후 run 시키기
go build main.go
./ main
executable file을 생성하지 않지만 프로그램에서 사용할 수 있는 코드가 들어있는 Go 패키지.
Reference:
https://medium.com/dev-genius/go-build-vs-go-run-baa3da9715cc
https://levelup.gitconnected.com/go-run-vs-go-build-vs-go-install-c7c0fd135cf9
https://medium.com/rungo/how-to-write-a-simple-go-program-13fd104f3018
go까지 GOOD!