Skip to content

Instantly share code, notes, and snippets.

@kirklewis
Created August 29, 2019 10:02
Show Gist options
  • Save kirklewis/e502c234fe6e88137544762dc419c644 to your computer and use it in GitHub Desktop.
Save kirklewis/e502c234fe6e88137544762dc419c644 to your computer and use it in GitHub Desktop.
Template File Processing in Go
package main
import (
"os"
"text/template"
)
func main() {
// variables
vars := make(map[string]interface{})
vars["Greeting"] = "Hello"
vars["Name"] = "Dev"
// parse the template
tmpl, _ := template.ParseFiles("templates/greeting.tmpl")
// create a new file
file, _ := os.Create("greeting.txt")
defer file.Close()
// apply the template to the vars map and write the result to file.
tmpl.Execute(file, vars)
}
{{.Greeting}} {{.Name}}!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment