title | keywords | |||||||
---|---|---|---|---|---|---|---|---|
Air Live Reloading |
|
This example demonstrates how to set up live reloading for a Go application using the Air tool. The purpose of this example is to show how to automatically reload your application during development whenever you make changes to the source code.
Live reloading is a useful feature during development as it saves time by automatically restarting the application whenever changes are detected. This example sets up a simple Fiber application and configures Air to watch for changes and reload the application.
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/air
-
Install the dependencies:
go mod download
-
Install Air:
go install github.com/cosmtrek/air@latest
Air is configured using the air/.air.conf
file. This file specifies the build command, binary name, and directories to watch for changes. The configuration files for different operating systems are provided:
air/.air.windows.conf
for Windowsair/.air.linux.conf
for Linux
To run the example with live reloading, use the following command:
air -c .air.linux.conf
or for Windows:
air -c .air.windows.conf
The server will start and listen on localhost:3000
. Any changes to the source code will automatically trigger a rebuild and restart of the application.
- GET /: Returns a simple greeting message.
package main
import (
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
// Create new Fiber instance
app := fiber.New()
// Create new GET route on path "/"
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
// Start server on http://localhost:3000
log.Fatal(app.Listen(":3000"))
}
This example provides a basic setup for live reloading a Go application using Air. It can be extended and customized further to fit the needs of more complex applications.