Skip to content

Instantly share code, notes, and snippets.

@jpillora
Created May 16, 2015 06:10
Show Gist options
  • Save jpillora/3540aeb53a6f20a29d0c to your computer and use it in GitHub Desktop.
Save jpillora/3540aeb53a6f20a29d0c to your computer and use it in GitHub Desktop.
Simple Templating in Go
package main
import (
"fmt"
"regexp"
)
func main() {
str := "my string and {{ foo }} and {{bar}}!"
str = template(str, map[string]string{"foo": "1", "bar": "2"})
fmt.Println(str)
}
var templateRe = regexp.MustCompile(`\{\{\s*(\w+)\s*\}\}`)
func template(str string, vars map[string]string) string {
return templateRe.ReplaceAllStringFunc(str, func(key string) string {
return vars[templateRe.FindStringSubmatch(key)[1]]
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment