Go Auto-Recompile and Reload Server on file change

后端 未结 9 912
清酒与你
清酒与你 2020-12-15 22:14

I know AppEngine does this, but I\'m not coding for it.

I tried using Guard from Ruby world, to listen on changes on .go files, and execut

9条回答
  •  隐瞒了意图╮
    2020-12-15 22:47

    After scrolling through the internet in search of a simple solution that was using standard linux tools (inotify & bash), I ended up creating this simple bash script that does the job.

    I tested it in a container running golang:1.12 and using go run . to serve files. read the script before using it, as it kills the go run processes depending on a folder name, and if there are conflicts with other processes that you run it might kill them.

    #!/bin/bash
    
    go run . &
    while inotifywait --exclude .swp -e modify -r . ;
    do
        # find PID of the file generated by `go run .` to kill it. make sure the grep does not match other processes running on the system
        IDS=$(ps ax | grep "/tmp/go-build" | grep "b001/exe/main" | grep -v "grep" | awk '{print $1}')
        if [ ! -z "$IDS" ]
        then
            kill $IDS;
        fi
        go run . &
    done;
    

提交回复
热议问题