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
+9.9k Golang : Turn string or text file into slice example
+7.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+20.2k Golang : How to get own program name during runtime ?
+31.7k Golang : How to convert(cast) string to IP address?
+9.7k Golang : Load ASN1 encoded DSA public key PEM file example
+26.3k Golang : Calculate future date with time.Add() function
+22.8k Golang : untar or extract tar ball archive example
+12.3k Golang : Validate email address
+9.1k Golang : Simple histogram example
+13k Swift : Convert (cast) Int to String ?
+19.1k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+11.6k Android Studio : Create custom icons for your application example