Air Live Reloading
Live Reloading with Air Example
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.
Description
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.
Requirements
Setup
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/airInstall the dependencies:
go mod downloadInstall Air:
go install github.com/air-verse/air@latest
Configuration
Air is configured with a config file that specifies the build command, binary name, and directories to watch for changes. There is one file per operating system:
air/.air.windows.conffor Windowsair/.air.linux.conffor Linux
Running the Example
To run the example with live reloading, use the following command:
air -c .air.linux.confor for Windows:
air -c .air.windows.confThe server will start and listen on localhost:3000. Any changes to the source code will automatically trigger a rebuild and restart of the application.
Example Routes
- GET /: Returns a simple greeting message.
Code Overview
main.go
package main
import (
"log"
"github.com/gofiber/fiber/v3"
)
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"))
}Conclusion
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.