Golang html/template.Must and New functions examples
package html/template
New allocates a new HTML template with the given name.
Must is a helper that wraps a call to a function returning (*Template, error) and panics if the error is non-nil. It is intended for use in variable initializations such as
var t = template.Must(template.New("name").Parse("html"))
Golang html/template.Must and New functions usage examples
Example 1:
var TimeNowForm = template.Must(template.New("").ParseFiles("time.html"))
Example 2:
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
t.Funcs(template.FuncMap{"trim": strings.TrimSpace})
template.Must(t.Parse(text))
if err := t.Execute(w, data); err != nil {
panic(err)
}
}
References :
https://github.com/beego/i18n/blob/master/beei18n/beei18n.go
Advertisement
Something interesting
Tutorials
+8k Golang : Sort words with first uppercase letter
+39.6k Golang : Remove dashes(or any character) from string
+18.7k Unmarshal/Load CSV record into struct in Go
+12.5k Golang : Forwarding a local port to a remote server example
+10.1k Golang : Get login name from environment and prompt for password
+6.1k Golang : Measure execution time for a function
+19.9k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+15.9k Golang : Update database with GORM example
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+15.3k Golang : Delete certain files in a directory
+16.3k Golang : Find out mime type from bytes in buffer
+20.2k Golang : Reset or rewind io.Reader or io.Writer