Home  >  Article  >  Backend Development  >  Share 5 ways to automatically reload Go programs when files change

Share 5 ways to automatically reload Go programs when files change

藏色散人
藏色散人forward
2020-11-03 15:19:582864browse

The following column will share with you 5 ways to automatically reload Share 5 ways to automatically reload Go programs when files change programs when files change. I hope it will be helpful to friends in need!

Many people hope to have the effect of real-time loading of code (hot compilation) when writing GO, especially those who are accustomed to using interpreted languages ​​such as JavaScript, Python and Ruby, this article Introduced 5 ways to reload Share 5 ways to automatically reload Go programs when files change programs in real time.

Share 5 ways to automatically reload Go programs when files change

This article assumes that the Share 5 ways to automatically reload Go programs when files change compiler has been installed and the

GOPATH/bin

path has been added to the PATH environment variable.

Before we begin, we first create a simple web server that can return the response content "Hello, World".

package mainimport (
    "net/http")func main() {
    http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, World"))
    })

    http.ListenAndServe(":5000", nil)}

Method 1: Using Air

Air is a command line program that provides real-time code loading for Share 5 ways to automatically reload Go programs when files change programs. Install Air by running the following commands.

go get -u github.com/cosmtrek/air

Next step, create the Air configuration file

.air.conf

in the root directory of the project.

# .air.conf
# toml配置文件来源于 [Air](https://github.com/cosmtrek/air)# 工作区间
# .(当前目录)或绝对路径, 注意这些目录都在根目录下面.root = "." tmp_dir = "tmp"[build]# 只是普通的shell命令。 可以使用`make`。
cmd = "go build -o ./tmp/main ."# `cmd`配置命令输出的二进制文件的位置。
bin = "tmp/main"# 自定义二进制输出。
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"# 监听的文件扩展后缀列表。
include_ext = ["go", "tpl", "tmpl", "html"]# 忽略这些文件扩展名或目录。
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]# 如果指定,则监听包含这些文件。
include_dir = []# 忽略文件列表.exclude_file = []# 如果文件修改太频繁,则不必在每次修改时都立刻触发构建,设置触发等待时间。
delay = 1000 # ms
# 发生编译错误时,是否停止旧的二进制程序。
stop_on_error = true# 该日志文件放置在tmp_dir中。
log = "air_errors.log"[log]# 日志是否显示时间
time = false[color]# 自定义每类输出的颜色。 如果找不到这个颜色,使用原本的日志输出演示。
main = "magenta"watcher = "cyan"build = "yellow"runner = "green"[misc]# 退出时是否删除临时目录
clean_on_exit = true

The configuration is simple and clear, please adjust it according to your project situation. Finally, instead of using the common

go run

command to run a Share 5 ways to automatically reload Go programs when files change program, use the

air

command to start the program. Method 2: docker to run Ari

This method requires the use of docker, if you don’t have it installed, you can follow We will still use the Air library , so the Air profile is still required. If you haven't already, please create a profile.

Docker image

cosmtrek/air

comes with the Air command installed, and the GOPATH environment variable is set to

/go

. We only need to mount our project directory into the GOPATH of the Docker container and use -p to expose the ports that need to be used. We can achieve this by running the docker run command: <pre class="brush:php;toolbar:false">docker run -it --rm -w &lt;working_dir&gt; -v &lt;project_folder&gt;:&lt;mount_point&gt; -p &lt;host_port&gt;:&lt;container_port&gt; &lt;image_name&gt;&lt;/image_name&gt;&lt;/container_port&gt;&lt;/host_port&gt;&lt;/mount_point&gt;&lt;/project_folder&gt;&lt;/working_dir&gt;</pre> In my case, I needed to run the following command:

docker run -it --rm -w /go/src/github.com/praveen001/live-reloading -v /go/src/github.com/praveen001/live-reloading:/go/src/github.com/praveen001/live-reloading -p 5000:5000 cosmtrek/air

Explanation:

Use the -v parameter to mount the project directory /home/praveen/go/src/github.com/praveen001/live-reloading to the directory in the GOPATH inside the container

/go/src/ github.com/praveen001/live-reloading

. <pre class="brush:php;toolbar:false">-v /home/praveen/go/src/github.com/praveen001/live-reloading:/go/src/github.com/praveen001/live-reloading</pre>Use the -w parameter to specify the mounting directory as the working directory.

-w /go/src/github.com/praveen001/live-reloading

The web server is listening on port 5000, so the -p flag needs to be used to expose container port 5000 to host port 5000.

-p 5000:5000

Finally, specify the docker image name cosmtrek/air.

Method 3: Using Gin

Gin is another command line program for live reloading of Share 5 ways to automatically reload Go programs when files change applications. Install Gin by running the following commands.

go get github.com/codegangsta/gin

Instead of running the application using the usual

go run main.go

command, use the

gin

command. In my case, the --appPort parameter tells Gin to listen on port 5000, and the

--port

parameter tells Gin to proxy to listen on port 3000. <pre class="brush:php;toolbar:false">gin --appPort 5000 --port 3000</pre>Now use the address http://localhost:3000 to access the Gin program.

If you want to exclude the listening directory, you can use the --excludeDir parameter, for example:

gin --appPort 5000 --port 3000 --excludeDir ./frontend

If you use Gin to load a program that does not start port listening, you must use the --immediate parameter. But Gin will still go to port 5000.

You can find all supported parameters in Gin's Github.

Method 4: Using Nodemon

Nodemon is another one for real-time Reload the command line program for Node applications. However, the startup command can be used to start other applications by using the --exec parameter.

Nodemon requires Nodejs and NPM to be installed. If it is not installed, you can install it according to the official documentation of nodejs. Run the following command to install nodemon:

npm install -g nodemon

Now, we can use Nodemon to run the web server by running the following command:

nodemon --exec go run main.go --signal SIGTERM

If you want to configure Nodemon, please create the configuration file

nodemon.json

in the root directory of the project. Complete working example configuration file

Method 5: Using Fresh

Fresh is another GO implementation for live reloading in Share 5 ways to automatically reload Go programs when files change Install Fresh

go get github.com/pilu/fresh

Instead of using the usual

go run main.go

command to run the application, use the

fresh

command. <pre class="brush:php;toolbar:false">fresh</pre>To configure Fresh, you need to create a configuration file runner.conf in the root directory of the project.

This is a sample configuration file. <pre class="brush:php;toolbar:false">root:              .tmp_path:          ./tmp build_name:        runner-build build_log:         runner-build-errors.log valid_ext:         .go, .tpl, .tmpl, .html no_rebuild_ext:    .tpl, .tmpl, .html ignored:           assets, tmp build_delay:       600colors:            1log_color_main:    cyan log_color_build:   yellow log_color_runner:  green log_color_watcher: magenta log_color_app:</pre>

Summary

There are many other tools, such as:
  • Facebook's Watchman
  • Realize
  • Reflex
  • Even a custom–built solution

Original address: https ://techinscribed.com/5-ways-to-live-reloading-go-applications/

Translation address: https://learnku.com/go/t/51189

The above is the detailed content of Share 5 ways to automatically reload Go programs when files change. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete