This is a template for a Go API project. It follows a standard directory structure and includes basic setup for an HTTP server, database connection (PostgreSQL), and routing.
cmd/api: Entry point of the application.internal/server: Server setup and route registration.internal/database: Database connection logic.
- Go 1.25+
- Docker (for database)
-
Clone the repository (if not already done).
-
Environment Variables: The application looks for a
.envfile (loaded viagodotenv). You can create one based on your needs. Example.env:PORT=8080 DB_URL=postgres://postgres:postgres@localhost:5432/templatedb?sslmode=disable
Note: The project uses
github.com/joho/godotenvto auto-load.envfiles locally. -
Run the Database: If you are running within the provided Devcontainer, a PostgreSQL instance is already running and configured (service
devdatabase).If running locally without the Devcontainer, ensure you have a PostgreSQL instance running and update
DB_URLaccordingly. -
Run the Application:
make run
The server will start on
http://localhost:8080(or the port specified in.env). -
Test the Endpoints:
- Health Check:
http://localhost:8080/health - Hello World:
http://localhost:8080/
- Health Check:
make build: Build the binary.make run: Run the application.make test: Run tests (includes database setup).make lint: Run linters (gofmtandgo vet).make fmt: Format code (go fmt).make watch: Run with live reload (usesair).make clean: Clean up artifacts.