Skip to content

Your First Template

This tutorial walks you through creating a simple Boilerplate template from scratch.

A project template that generates a Go project with:

  • A customizable project name and description
  • An optional Dockerfile
  • A README.md
  1. Create the template directory

    Terminal window
    mkdir -p my-go-template
    cd my-go-template
  2. Create boilerplate.yml

    This is the configuration file that defines your template’s variables:

    boilerplate.yml
    variables:
    - name: ProjectName
    description: Name of the Go project
    type: string
    validations:
    - required
    - name: Description
    description: A short project description
    type: string
    - name: IncludeDocker
    description: Include Dockerfile?
    type: bool
    default: false
    skip_files:
    - path: Dockerfile
    if: "{{ not .IncludeDocker }}"
  3. Create README.md

    # {{ .ProjectName }}
    {{ .Description }}
    ## Getting Started
    ```bash
    go run main.go
    ```
  4. Create main.go

    package main
    import "fmt"
    func main() {
    fmt.Println("Hello from {{ .ProjectName }}!")
    }
  5. Create Dockerfile

    FROM golang:1.21-alpine
    WORKDIR /app
    COPY . .
    RUN go build -o /{{ .ProjectName | dasherize }} .
    CMD ["/{{ .ProjectName | dasherize }}"]
  6. 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
  7. Answer the prompts for the ProjectName variable.

  8. Check the output

    Terminal window
    ls my-project/
    # Dockerfile README.md main.go

    The files will have all {{ .ProjectName }} references replaced with whatever value you entered.

When you ran the command, Boilerplate:

  1. Read boilerplate.yml to discover variables and settings
  2. Used the --var flags to fill in variable values
  3. Prompted you for any missing values, in this case the value of the ProjectName variable
  4. Rendered each file through the Go template engine, replacing {{ .ProjectName }}, {{ .Description }}, etc.
  5. Evaluated the skip_files condition — since IncludeDocker was true, the Dockerfile was included
  6. Wrote the rendered files to the output folder