Go Auto-Recompile and Reload Server on file change

怎甘沉沦 提交于 2019-12-22 03:54:08

问题


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 execute the following commands:

killall foo
go build -race
./foo &

But it never sends foo into background, it just hangs indefinitely.

How are you guys solving this problem? Solution has to be cross-platform too (GNU/Linux and Mac).


回答1:


A friend wrote a simple Compile Daemon for go, worked like a charm for my own small net/http-projects.

You can find the repository here: https://github.com/githubnemo/CompileDaemon




回答2:


You can also try out Gin by Codegangsta. It's fire and forget.

https://github.com/codegangsta/gin

EDIT: I prefer CompileDaemon nowadays. Gin sometimes won't accept requests




回答3:


I've recently discovered a reflex tool. It's fast and works like a charm. It is very similar to nodemon (from nodejs world) and guard (from ruby world).

Most of the time I'm using it similar to below:

reflex -d none -s -R vendor. -r \.go$ -- go run cmd/server/main.go

But it maybe more convenient to have it's options in a file like .reflex, with contents like this:

-d none -s -R vendor. -r \.go$

So then you just run it like this

reflex $(cat .reflex) -- go run cmd/server/main.go

You can do same thing to "hot reload" tests:

reflex $(cat .reflex) -- go test ./... -v

There is also a config option where you can specify a number of commands you run same time, but I don't really use it.




回答4:


You can use nodemon for this. Simply create a nodemon.json file containing your configuration, files to watch, files to ignore, and command to execute when a file changes. Something like this configuration.

nodemon.json

{
  "watch": ["*"],
  "ext": "go graphql",
  "ignore": ["*gen*.go"],
  "exec": "go run scripts/gqlgen.go && (killall -9 server || true ) && go run ./server/server.go"
}

You do require nodejs for this to work.
But its far better then any other tool I've used so far that are go specific.




回答5:


if anyone’s still looking for a solution, i wrote some shell scripts to do this and it’s usable via a docker environment, the repos at https://github.com/zephinzer/golang-dev




回答6:


there are 2 main contenders here in GO world fresh & glide

But I will go with Fresh https://github.com/gravityblast/fresh



来源:https://stackoverflow.com/questions/19605076/go-auto-recompile-and-reload-server-on-file-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!