Go 에서 웹서버를 개발하는데 vuejs의 핫 리로딩같은 기능이 있으면 좋겠다고 생각하다가 찾아보게 됐습니다!
스프링에서도 글자 하나 바꿔도 빌드를 다시해야 했던 경험이 있어서 그런 불편함을 다시 느끼고 싶지 않았습니다 🥹
버튼 눌러서 다시 빌드하고 기다리고 폰보고 암튼 기다리는건 너무 지루하단 말이죠?
그렇다면 이럴 때 필요한 건? Live Reloading
이죠.
air
를 시작 후 디렉토리 생성 가능vuejs 에서는 Hot Reload, air는 Live Reload 무슨 차이인지 궁금해서 찾아보니, 역시 스택오버플로우 에 나와있습니다!
파일이 변경되면 전체 앱을 다시 로드하거나 새로 고칩니다.
파일이 변경되면 변경된 파일만 새로 고칩니다.
go install
명령어를 입력해서 설치할 수 있습니다.
저는 Goland 환경에서 Gopath 경로를 ~/sdk 로 설정하고 있습니다.
go install github.com/cosmtrek/air@latest
셸 스크립트를 사용해서 air 패키지를 설치하는 방법입니다.
근데 전 잘 몰라서 go install
로 설치했습니다!
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s
air -v
go 파일을 만들고 main function을 작성합니다.
package main
import "fmt"
func main() {
fmt.Println("hello")
}
그리고 air
커맨드를 입력해줍니다
air
air
가 실행되면 tmp 폴더가 생성되면서 라이브 리로딩이 시작됩니다!
여기서 tmp 폴더에는 빌드된 바이너리(파일)가 저장되는 경로입니다
cloq@cloqui-MacBookPro air-example % air
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ , built with Go
mkdir ~/GolandProjects/air-example/tmp
watching .
!exclude tmp
building...
running...
hello
자 그럼 이제 코드 내용을 수정해보겠습니다
package main
import "fmt"
func main() {
fmt.Println("hello kwanok!")
}
main.go 가 바꼈다고 하면서 제가 바꾼 문자열이 출력되는 걸 확인할 수 있습니다 👍
main.go has changed
building...
running...
hello kwanok!
아래 명령어를 치면 .air.toml
파일이 생성됩니다
air init
.air.toml
파일을 열어보니 다양한 설정이 가능할 것으로 보이는데요
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
kill_delay = "0s"
log = "build-errors.log"
send_interrupt = false
stop_on_error = true
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false
만약 clean_on_exit
를 true
로 설정한다면 air
가 종료될 때 앞에 생성됐던 tmp
디렉토리가 삭제됩니다
이런 설정을 적용하기 위해서 명령어 뒤에 -c {설정 파일 이름} 을 붙여줘야 합니다
air -c .air.toml
더 많은 예시는 설정 파일 예시 에서 확인할 수 있습니다 :)