An Interest In:
Web News this Week
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
Air: Live reload on Golang applications
Go is a programming language with advanced features and clean syntax. It has a robust and well-documented common library, and has focus on good software engineering and principles.
Although Go is a great language, there's one downside: it needs to be recompiled every time after a single line change.
So, to make our lives easier in this post we're gonna learn how to use Air as our live reload.
App
Init with Go mod
go mod init helloworld
Creating App Go file
package mainimport ( "fmt" "net/http")func main() { port := 3000 fmt.Println("Starting Server on port", port) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, World")) }) http.ListenAndServe(":3000", nil)}
Air:
Installing Air.
go get -u github.com/cosmtrek/air
Air configuration file
air init
Starting Air.
air
You'll have the following result:
Now after any file change Air will reload your app automatically.
Air and Docker
First we need to create a Dockerfile
# DockerfileFROM cosmtrek/airRUN mkdir -p /app/distCOPY ./ /app/WORKDIR /app/RUN go get -d -v ./...RUN go install -v ./...CMD ["air"]
Now we need to create a docker-compose to make our life easier
# docker-compose.ymlversion: '3.7'services: app: build: . ports: - '3000:3000' volumes: - ./:/app/
Now you just need to run docker-compose up
And you'll have the following result:
References:
- Air: https://github.com/cosmtrek/air
- Golang-book: https://www.golang-book.com/books/intro/1
Original Link: https://dev.to/brenoalves/air-live-reload-on-golang-applications-1fgk
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To