TIL # 89 : [GO] Go Command lines

셀레스틴 허·2021년 3월 13일
post-thumbnail

go린이의 첫걸음

package main 

import "fmt"

func man() {
	fmt.Println("Hello World")
}

go build main.go

해당 프로그램은 "Hello World"를 항상 프린트하는 executable file을 만든다. 프로그램을 다시 run하고 싶을 때 프로그램을 compile할 필이 없이 그냥 executable file을 run하면 된다. 즉 사용자와 상호작용하는 빠른 코드를 원한다면 프로그램을 한번 compile하고 executable file을 쓰면 된다.

go run main.go

프로그램을 수정하고 싶으면 경우에는 어떻게 할까? go build로 다시 다른 executable file을 compile하고 run하는 것은 비효율적이다. 작은 에러 하나 발견할 때마다 이렇게 진행하는 것은 대단히~ 비효율적이다. 이 때 go run을 쓰면 된다. go run은 compile + execution을 둘 다 처리해주기 때문이다. 새롭게 고친 코드의 아웃풋을 확인할 때 매우 유용하다.

go run은 executable file을 현재 폴더에 생성 안된다.

go build -o "디렉토리 경로"

compile된 binary를 특정 경로에 아웃풋 되게끔 해준다.

go install

go build와 똑같은 작업을 하지만 binary를 $GOPATH/bin 디렉토리에, go get을 통해 설치했던 tool들 사이에 추가한다. 이제 $GOPATH/bin/hello을 하면 터미널에 Hello, world가 뜰 것이다.

Compile하는 이유는?
Go 코드를 컴퓨터 언어 -> binary code로 변환시키기 위해!

go build & run!

executable file 생성 후 run 시키기

go build main.go
./ main

library in Go?

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

profile
Software Developer / 고통은 필연, 괴로움은 선택

2개의 댓글

comment-user-thumbnail
2021년 3월 13일

go까지 GOOD!

1개의 답글