在文件更改时自动重新编译和重新加载服务器

huangapple go评论76阅读模式
英文:

Go Auto-Recompile and Reload Server on file change

问题

我知道AppEngine可以做到这一点,但我不是为它编写代码。

我尝试使用Ruby世界中的Guard来监听.go文件的更改,并执行以下命令:

killall foo
go build -race
./foo &

但它从未将foo发送到后台,而是无限期地挂起。

你们是如何解决这个问题的?解决方案还必须是跨平台的(GNU/Linux和Mac)。

英文:

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

得分: 32

另一个选项是,如果你的机器上已经安装了Node.js,

使用以下命令全局安装nodemonnpm i -g nodemon

进入你的代码目录并运行:

nodemon --watch './**/*.go' --signal SIGTERM --exec 'go' run cmd/MyProgram/main.go

这将在任何.go文件更改时发送SIGTERM信号,并运行go run cmd/MyProgram/main.go命令。

完全跨平台。

英文:

Another option, if you have nodejs installed on your machine

install nodemon with -g npm i -g nodemon

go to your code dir and run:

nodemon --watch './**/*.go' --signal SIGTERM --exec 'go' run cmd/MyProgram/main.go

This will send SIGTERM every time any .go files changes and will run go run cmd/Myprogram/main.go

Fully cross platform.

答案2

得分: 29

一个朋友为Go语言写了一个简单的编译守护程序,对我的小型net/http项目非常有效。

你可以在这里找到该仓库:https://github.com/githubnemo/CompileDaemon

英文:

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

答案3

得分: 17

这里有一个go版本的nodemon:https://github.com/mitranim/gow

go install github.com/mitranim/gow@latest

用法

# 开始并在更改后重新启动
gow run .

# 将参数传递给程序
gow run . arg0 arg1 ...

# 运行子目录
gow run ./subdir

# 在更改后进行检查和重新检查建议使用详细模式
gow -v vet

# 重新启动时清除终端
gow -c run .

# 指定要监视的文件扩展名
gow -e=go,mod,html run .

# 帮助
gow -h
英文:

There is a go version of nodemon: https://github.com/mitranim/gow

go install github.com/mitranim/gow@latest

usage

# Start and restart on change
gow run .

# Pass args to the program
gow run . arg0 arg1 ...

# Run subdirectory
gow run ./subdir

# Vet and re-vet on change; verbose mode is recommended
gow -v vet

# Clear terminal on restart
gow -c run .

# Specify file extension to watch
gow -e=go,mod,html run .

# Help
gow -h

答案4

得分: 16

我最近发现了一个名为reflex的工具。它快速而且非常好用,与Node.js世界的nodemon和Ruby世界的guard非常相似。

大部分时间我都是这样使用它的:

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

但是将它的选项放在一个名为.reflex的文件中可能更方便,文件内容如下:

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

然后你可以这样运行它:

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

你也可以用同样的方式来进行“热重载”测试:

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

还有一个配置选项,可以指定同时运行的命令数量,但我并没有真正使用它。

英文:

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.

答案5

得分: 14

你也可以尝试使用Codegangsta的Gin。它非常方便。

https://github.com/codegangsta/gin

编辑:
我现在更喜欢使用CompileDaemon。有时候Gin不会接受请求。

英文:

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

答案6

得分: 5

我使用了一个叫做entr的工具。

要安装它,在Mac上运行brew install entr,在Linux上运行sudo apt-get install entr

要在.go文件更改后重新编译和运行,运行以下命令...

ls **/*.go | entr go run main.go
英文:

I used a tool called entr

To install brew install entr (mac)
or, sudo apt-get install entr (linux)

To recompile & run on .go file changes, run the following command ...

ls **/*.go | entr go run main.go

答案7

得分: 5

最好的选择是在您的计算机上安装nodejs并使用nodemon包。

使用以下命令安装nodemonsudo npm install -g nodemon
还要使用sudo以避免任何写入权限问题。

现在进入您的程序目录并运行:

nodemon --watch './**/*.go' --signal SIGTERM --exec 'go' run path-to-the-main-file-of-go-program-with-extension

这将在任何.go文件更改时发送SIGTERM信号,并运行go run main-go-file-of-program-with-extension

完全跨平台。
只需更改命令,即可适用于任何编程语言,如下所示:

nodemon --watch './**/*.extension-of-programming-file-without-preceeding-dot' --signal SIGTERM --exec 'go' run path-to-the-main-file-of-program-with-extension
英文:

Best option is to install nodejs on your machine and use nodemon package

install nodemon with -g sudo npm install -g nodemon
Also use sudo to avoid any write permission

Now go to your program dir and run:

nodemon --watch './**/*.go' --signal SIGTERM --exec 'go' run path-to-the-main-file-of-go-program-with-extension

This will send SIGTERM every time any .go files changes and will run go run main-go-file-of-program-with-extension

Fully cross platform.
This will work for any programming language by just changing the command as

nodemon --watch './**/*.extension-of-programming-file-without-preceeding-dot' --signal SIGTERM --exec 'go' run path-to-the-main-file-of-program-with-extension

答案8

得分: 4

你可以使用nodemon来实现这个功能。只需创建一个nodemon.json文件,其中包含你的配置、要监视的文件、要忽略的文件以及文件更改时要执行的命令。配置文件的示例如下:

nodemon.json

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

你需要安装Node.js才能使用nodemon。但它比我迄今为止使用过的任何其他针对Go的工具都要好得多。

英文:

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. <br />But its far better then any other tool I've used so far that are go specific.

答案9

得分: 2

这里在GO世界中有两个主要的竞争者,freshglide

但我会选择Fresh,网址是https://github.com/gravityblast/fresh。

英文:

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

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

答案10

得分: 2

在浏览互联网寻找使用标准Linux工具(inotify和bash)的简单解决方案后,我最终创建了这个简单的bash脚本来完成任务。

我在运行golang:1.12的容器中进行了测试,并使用go run .来提供文件。在使用之前,请阅读脚本,因为它会根据文件夹名称终止go run进程,并且如果与您运行的其他进程发生冲突,可能会终止它们。

#!/bin/bash

go run . &
while inotifywait --exclude .swp -e modify -r . ;
do
    # 查找由`go run .`生成的文件的PID以终止它。确保grep不匹配系统上运行的其他进程
    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;

希望对你有帮助!

英文:

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 . &amp;
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 &quot;/tmp/go-build&quot; | grep &quot;b001/exe/main&quot; | grep -v &quot;grep&quot; | awk &#39;{print $1}&#39;)
	if [ ! -z &quot;$IDS&quot; ]
	then
		kill $IDS;
	fi
	go run . &amp;
done;

答案11

得分: 1

在Linux环境中编写Go代码时,您可以在本地不安装任何额外程序的情况下监视已更改的文件并自动重新启动。

find . | grep '\.go' | entr -r go run .
英文:

While writing go in linux environment, you can watch the changed files and restart automatically without installing any additional programs in the local.

find . | grep &#39;\.go&#39; | entr -r go run .

答案12

得分: -1

如果还有人在寻找解决方案,我写了一些Shell脚本来完成这个任务,并且可以通过Docker环境来使用。仓库地址是https://github.com/zephinzer/golang-dev。

英文:

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

huangapple
  • 本文由 发表于 2013年10月26日 17:31:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/19605076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定