Your First Template
This tutorial walks you through creating a simple Boilerplate template from scratch.
What You’ll Build
Section titled “What You’ll Build”A project template that generates a Go project with:
- A customizable project name and description
- An optional
Dockerfile - A
README.md
Tutorial
Section titled “Tutorial”-
Create the template directory
Terminal window mkdir -p my-go-templatecd my-go-template -
Create
boilerplate.ymlThis is the configuration file that defines your template’s variables:
boilerplate.yml variables:- name: ProjectNamedescription: Name of the Go projecttype: stringvalidations:- required- name: Descriptiondescription: A short project descriptiontype: string- name: IncludeDockerdescription: Include Dockerfile?type: booldefault: falseskip_files:- path: Dockerfileif: "{{ not .IncludeDocker }}" -
Create
README.md# {{ .ProjectName }}{{ .Description }}## Getting Started```bashgo run main.go``` -
Create
main.gopackage mainimport "fmt"func main() {fmt.Println("Hello from {{ .ProjectName }}!")} -
Create
DockerfileFROM golang:1.21-alpineWORKDIR /appCOPY . .RUN go build -o /{{ .ProjectName | dasherize }} .CMD ["/{{ .ProjectName | dasherize }}"] -
Run the template
Terminal window boilerplate \--template-url ./my-go-template \--output-folder ./my-project \--var Description="Boilerplate is kind of cool" \--var IncludeDocker=true -
Answer the prompts for the
ProjectNamevariable. -
Check the output
Terminal window ls my-project/# Dockerfile README.md main.goThe files will have all
{{ .ProjectName }}references replaced with whatever value you entered.
What Happened
Section titled “What Happened”When you ran the command, Boilerplate:
- Read
boilerplate.ymlto discover variables and settings - Used the
--varflags to fill in variable values - Prompted you for any missing values, in this case the value of the
ProjectNamevariable - Rendered each file through the Go template engine, replacing
{{ .ProjectName }},{{ .Description }}, etc. - Evaluated the
skip_filescondition — sinceIncludeDockerwastrue, theDockerfilewas included - Wrote the rendered files to the output folder
Next Steps
Section titled “Next Steps”- Learn about all variable types and validations
- Chain templates together with dependencies
- Explore the full Go template syntax