Basics
Basics
- Routing: Map URLs to handlers, group routes under shared prefixes and middleware, name them, and generate URLs from names.
- Handlers and requests: Write handlers that receive typed input, return typed output, and turn errors into proper HTTP responses.
- Render HTML with templ: Write pages as templ components (typed, compiled, escaped by default), share a layout, link to routes by name, serve static files with cache-busting URLs, and add interactivity with the bundled htmx.
- Style your app: A new project looks finished from the first page. Its pages are made of components in
views/ui, a package of your app, styled by one stylesheet,public/static/app.css. Change the colors, change a component, add your own, start with Pico, Bootstrap or Bulma, or start without styles. - Handle HTML forms: Protect forms against cross-site request forgery, send PUT and DELETE from plain HTML, and show validation errors next to the fields with the submitted values kept.
- Validation: Declare rules on an input struct, get one clear message per field, and add checks of your own.
- Sessions and flash messages: Remember things about a visitor between requests, and show a message once after a redirect.
- Configure your application: Read settings from
.envfiles and the environment into typed Go structs, with defaults, required keys and validation. - Commands: Your application builds to one binary. Its first argument picks what it does: run the app (the default), run only some process types, migrate the database, list the routes, or run commands of your own.
- Test your app: Boot the app in a test, send it requests like a browser or an API client, and check the responses, the session, the database, the jobs, events and email the app sent, and its files, with
anetostest; freeze or move the app’s clock. - Describe an API with OpenAPI: Package
web/openapiwrites an OpenAPI 3.1 document of your API from its routes and its typed handlers: what each operation reads and answers, which credentials it needs and how it fails. You commit the document (openapi.json) beside the code, a test fails when it’s out of date, and the app serves it for clients and tools.