This project demonstrates how to set up hot reloading for both Go templates and Tailwind CSS in a web application.
Before you begin, make sure you have the following installed:
- Go (1.16 or later)
- Node.js and npm
- Air (Go hot reload tool):
go install github.com/cosmtrek/air@latest
├── .air.toml # Air configuration for Go hot reloading
├── go.mod # Go module file
├── main.go # Main Go application
├── package.json # Node.js dependencies and scripts
├── tailwind.config.js # Tailwind CSS configuration
├── static/ # Static assets
│ └── css/
│ ├── input.css # Source CSS with Tailwind directives
│ └── styles.css # Generated CSS (will be created)
└── templates/ # HTML templates
└── index.html # Main template file
- Install Node.js dependencies:
npm installnpm install tailwindcss npm run tailwind:watch- Build the initial Tailwind CSS file:
npm run tailwind:buildTo start the application with hot reloading for both Go and Tailwind CSS:
npm run devThis command will:
- Watch for changes in your Go files and templates and automatically rebuild and restart the server
- Watch for changes in your CSS and HTML files and automatically rebuild the Tailwind CSS
Your application will be available at http://localhost:8080
Air watches for changes in your Go files and templates. When a change is detected, it automatically rebuilds and restarts your application.
The Tailwind CLI watches your CSS input file and template files for changes. When a change is detected, it automatically rebuilds the CSS output file.
The concurrently package is used to run both the Air and Tailwind CLI watch commands simultaneously.
- Edit the Go files or templates to see the Go hot reloading in action
- Edit the CSS or add Tailwind classes to the HTML to see the Tailwind CSS hot reloading in action
For production, you should build an optimized version of your Tailwind CSS:
npm run tailwind:buildThen build and run your Go application as usual:
go build -o app .
./app