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
+7k Android Studio : Hello World example
+14.3k Golang : Fix image: unknown format error
+5.9k Golang : Find change in a combination of coins example
+14.1k Golang : convert rune to unicode hexadecimal value and back to rune character
+9.1k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+6.4k Golang : How to get capacity of a slice or array?
+14.7k Golang : Missing Bazaar command
+30k Golang : How to get HTTP request header information?
+16.5k Golang : Loop each day of the current month example
+6.4k Javascript : Generate random key with specific length
+7.6k Golang : Get YouTube playlist
+40.3k Golang : UDP client server read write example