Golang html/template.Template.Parse function example
package html/template
Parse parses a string(1st parameter) into a template. Nested template definitions will be associated with the top-level template t. Parse may be called multiple times to parse definitions of templates to associate with t. It is an error if a resulting template is non-empty (contains content other than template definitions) and would replace a non-empty template with the same name. (In multiple calls to Parse with the same receiver template, only one call can contain text other than space, comments, and template definitions.)
Golang html/template.Template.Parse function usage example
package main
import (
"fmt"
"html/template"
"os"
)
var defaultTemplate string = `<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ .Title }}</title>
</head>`
func main() {
t := template.New("HELLO")
t.Parse(defaultTemplate) // <--- here
data := map[string]interface{}{
"Title": "Hello World!",
}
t.Execute(os.Stdout, data) // output to screen
fmt.Println("\n\n")
fmt.Println("Template name is : ", t.Name())
}
Output :
<!DOCTYPE html> <html lang="en"> <head> <title>Hello World!</title> </head> Template name is : HELLO
Reference :
Advertisement
Something interesting
Tutorials
+18.2k Golang : Convert IPv4 address to decimal number(base 10) or integer
+11.4k Golang : How to pipe input data to executing child process?
+14.6k Golang : How to pass map to html template and access the map's elements
+5.7k Golang : Shortening import identifier
+15.5k Golang : invalid character ',' looking for beginning of value
+6.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+4.7k JavaScript : Rounding number to decimal formats to display currency
+17.5k Golang : Get future or past hours, minutes or seconds
+7.2k Nginx : How to block user agent ?
+9.6k Golang : Validate IPv6 example
+12.1k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+25.8k Golang : missing Mercurial command