How to use air to overload code automatically

Time:2021-5-9

Air can monitor the code of the project in real time, and automatically recompile and execute after the code changes

Install air (Windows)

(1) , inhttps://github.com/cosmtrek/air/releasesYou can download the air at, put it into the bin directory under the go installation directory, and rename it toair.exe

(2) In the windows command window, you can also usecurl -fLo air.exe https://git.io/windows_airCommand to install air (access the Internet, enable go module, set go proxy to accelerate)

After installation, we can use it in the built-in command line terminal of GoLandair -vCommand to check whether the installation is successful:

How to use air to overload code automatically

Use and test air

adoptairCommand enable air

How to use air to overload code automatically

Run the following code:

package main

import (
    "fmt"
    "net/http"
)

func handlerFunc(w http.ResponseWriter, r *http.Request) {

    FMT. Fprint (W, "< H1 > air automatic overload < H1 >")
}

func main(){
    http.HandleFunc("/", handlerFunc)
    http.ListenAndServe(":3030", nil)
}

Access in browserlocalhost:3030/display

How to use air to overload code automatically

Modify the code

package main

import (
    "fmt"
    "net/http"
)

func handlerFunc(w http.ResponseWriter, r *http.Request) {

    FMT. Fprint (W, "< H1 > air automatic overload < H1 >")
}

func main(){
    http.HandleFunc("/", handlerFunc)
    http.ListenAndServe(":3000", nil)
}

Visit separatelylocalhost:3030/localhost:3000/The results are as follows

How to use air to overload code automatically

How to use air to overload code automatically

Code version

Use the command to view the file status:

$ git status

How to use air to overload code automatically

It’s not hard to find that it appears in the root of the projecttmpDirectory, which is where the air compiled files are stored. We need to set the version controller to betmpThe directory is excluded.

Create a new one in the root directory.gitignoreFile, which indicates which files and directories git will ignore when you commit. Once created, thetmpCatalog added to.gitignoreFile:

How to use air to overload code automatically

At this point, we can use the command to check the file status and find that the TMP directory is excluded

How to use air to overload code automatically

The above is the use of air automatic overload in go projects.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Go cannot find package “go-sql-driver/mysql” in any of

The solution of cannot find package “GitHub. COM / go SQL driver / MySQL” in any of The main problem is lack of informationgo.modDocuments.To solve the problem, you need to initialize ago.modDocuments; Use the following command: go mod init Gone This work adoptsCC agreementReprint must indicate the author and the link of this article