This repository demonstrates a clean and practical project structure for building web applications in Go, using Go's standard library templates (html/template) for the frontend and Go for backend logic.
Note: This structure reflects my personal preference and workflow. It works well for many web projects, but you might want to adapt or extend it depending on your specific requirements.
.
├── main.go # Application entry point
├── server/
│ └── server.go # HTTP server setup and start
├── config/
│ ├── port.go # Server port and configuration constants
│ └── paths.go # Paths to templates and static assets
├── routes/
│ └── router.go # HTTP route definitions and handlers
├── controllers/
│ ├── render/ # Template rendering logic
│ │ └── renderPage.go
│ ├── homeController.go # Controller for home page
│ ├── aboutController.go # Controller for about page
│ ├── contactController.go # Controller for contact page
│ ├── helpController.go # Controller for help page
│ └── errorController.go # Controller for error pages
├── models/ # Data models and structs
├── views/
│ ├── pages/ # Full page templates (layout + content)
│ │ ├── layout.html # Main layout template
│ │ ├── home.html
│ │ ├── about.html
│ │ ├── contact.html
│ │ ├── help.html
│ │ └── error.html
│ └── sections/ # Reusable template sections (partials)
│ └── navbar.html # Navigation bar partial
├── go.mod # Go module file
└── go.sum # Go module checksums
- Separation of Concerns: Controllers handle HTTP requests and business logic, views handle the UI templates, and routes define URL mappings.
- Reusable Templates: Using partials like
navbar.htmlhelps avoid duplication and makes templates easier to maintain. - Scalability: Adding new pages or components is straightforward—just add a new controller and page template.
- Config Centralization: Server port and path constants are kept in the
configfolder, making global changes easier. - Readability & Maintainability: Clear folder naming and file organization make the codebase approachable for you or collaborators.
-
Static Assets (CSS, JS, images):
Create a/static/folder at the root (e.g.,./static/css/,./static/js/) and serve it usinghttp.FileServerin your server setup.
Update your templates to link stylesheets like:<link rel="stylesheet" href="/static/css/style.css">
-
Middleware:
When you need logging, authentication, recovery, or compression, add amiddlewares/package and wrap your handlers accordingly. -
Models:
Organize your data structures, database access, and business logic in themodels/package. This keeps your application’s core data logic clean and reusable. -
Template Caching:
For performance, parse and cache templates once during app start instead of parsing on every request. -
Environment Variables:
Use.envor OS environment variables for configuration (port, database URLs) rather than hardcoding inconfig/. -
Modularization:
As your project grows, consider splitting controllers and models into feature-specific packages (e.g.,user/,product/). -
Error Handling:
Centralize error handling and create custom error pages.
- Put your Go source files as described above.
- Put HTML templates in
views/pages/and partials inviews/sections/. - Add static assets (CSS/JS/images) in a dedicated
static/folder. - Serve your static folder from Go like:
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
- Use
RenderPagein controllers to render templates. - Run your app and visit
http://localhost:<port>.
This structure is a great starting point for Go web applications that use server-side rendering with Go templates. It balances simplicity with organization, making it easy to extend and maintain.
Feel free to fork, adapt, and improve it for your own projects!
If you find this helpful, please ⭐ the repo and share with others looking for a clean Go web project structure!
Happy coding! 🚀