Introduction to the

Air is a hot loading tool for Go language. It can listen for changes in files or directories, automatically compile and restart programs. Greatly improve the work efficiency during development period.

Quick to use

This code uses Go Modules, running on a Mac.

Create a directory and initialize it:

$ mkdir air && cd air
$ go mod init github.com/darjun/go-daily-lib/air
Copy the code

Execute the following command to install the AIR tool:

$ go get -u github.com/cosmtrek/air
Copy the code

The above command generates the air command in the $GOPATH/bin directory. I typically add $GOPATH/bin to the system PATH, so I can easily execute air anywhere.

Here we write a simple Web server using the standard library NET/HTTP:

package main

import (
  "fmt"
  "log"
  "net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintln(w, "Hello, world!")}func main(a) {
  mux := http.NewServeMux()
  mux.HandleFunc("/", index)

  server := &http.Server{
    Handler: mux,
    Addr:    ": 8080",
  }

  log.Fatal(server.ListenAndServe())
}
Copy the code

Run the program:

$ go run main.go
Copy the code

Localhost :8080 in your browser to see Hello, world! .

If I were to ask you to change Hello, World! Instead of Hello, DJ! If you don’t use AIR, you can only modify the code. Run go Build to compile and restart.

With AIR, we just need to execute the following command to run the program:

$ air
Copy the code

Air automatically compiles, starts the program, and listens for file changes in the current directory:

When we put Hello, World! Instead of Hello, DJ! When, AIR listens for the change, automatically compiles, and restarts the program:

At this point, we go to localhost:8080 in the browser and the text changes to Hello, DJ! Is it very convenient?

configuration

Run the air command directly, using the default configuration. It is recommended that you copy the air_example.toml configuration file provided in the AIR project and modify and customize it according to your own requirements:

root = "."
tmp_dir = "tmp"

[build]
cmd = "go build -o ./tmp/main ."
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 = []
log = "air.log"
delay = 1000 # ms
stop_on_error = true
send_interrupt = false
kill_delay = 500 # ms

[log]
time = false

[color]
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
clean_on_exit = true
Copy the code

You can configure the project root directory, temporary file directory, commands to compile and execute, listen file directory, listen suffix, and even console log color.

Debug mode

If you want to see a more detailed air execution process, you can use the -d option.

With the -d option, AIR outputs very detailed information to help troubleshoot problems.

conclusion

Using AIR eliminates frequent compilations and restarts during development. Automating all of this greatly increases development efficiency.

If you find a fun and useful Go library, please Go to GitHub and submit issue😄

reference

  1. Air GitHub:github.com/cosmtrek/ai…
  2. GitHub: github.com/darjun/go-d…

I

My blog is darjun.github. IO

Welcome to follow my wechat public account [GoUpUp], learn together, progress together ~