Documentation › Traditional web application › Helper functions
Your generated codebase will contain a cmd/web.helpers.go file which contains miscellaneous helper functions for the code in the cmd/web package.
The exact contents of this file will vary depending on the options you select, but it may include the following functions:
| Function | Description |
|---|---|
newTemplateData() |
Returns a map containing common data for use in HTML templates. |
newEmailData() |
Returns a map containing common data for use in email templates. |
backgroundTask() |
Runs a function in a background goroutine, automatically recovering from panics and reporting any returned errors. Used for running background tasks. |
You should feel free to create any additional helper functions that you need in this file. If your helper needs access to a dependency – such as the database or logger – you should implement the helper as a method on the application struct.
func (app *application) exampleHelper() {
// Helper code goes here...
}