Created
August 29, 2019 10:02
-
-
Save kirklewis/e502c234fe6e88137544762dc419c644 to your computer and use it in GitHub Desktop.
Template File Processing in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{.Greeting}} {{.Name}}! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment