Golang html/template.Template.ParseGlob function example
package html/template
ParseGlob parses the template definitions in the files identified by the pattern(1st parameter - a.k.a wild card) and associates the resulting templates with t. The pattern is processed by filepath.Glob and must match at least one file. ParseGlob is equivalent to calling t.ParseFiles with the list of files matched by the pattern.
Golang html/template.Template.ParseGlob function usage example
package main
import (
"fmt"
"html/template"
"os"
)
func main() {
t := template.New("HELLO")
var templates = template.Must(t.ParseGlob("*.html")) // we look for defaultTemplate.html file
data := map[string]interface{}{
"Title": "Hello World!",
}
templates.ExecuteTemplate(os.Stdout, "defaultTemplate.html", data)
fmt.Println("\n\n")
fmt.Println("Template name is : ", t.Name())
}
content of defaultTemplate.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ .Title }}</title>
</head>
Output :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World!</title>
</head>
Template name is : HELLO
References :
https://www.socketloop.com/references/golang-html-template-parseglob-function-examples
See also : Golang html/template.ParseGlob function examples
Advertisement
Something interesting
Tutorials
+9.5k Golang : Convert(cast) string to int64
+14.2k Elastic Search : Mapping date format and sort by date
+28.2k Golang : Connect to database (MySQL/MariaDB) server
+19.2k Golang : Check if directory exist and create if does not exist
+7.5k Golang : Create zip/ePub file without compression(use Store algorithm)
+10.4k Golang : Generate random integer or float number
+7.4k Golang : Convert source code to assembly language
+26.1k Mac/Linux and Golang : Fix bind: address already in use error
+16.3k Golang : convert string or integer to big.Int type
+12.3k Golang : Display list of countries and ISO codes
+19.2k Golang : Check whether a network interface is up on your machine
+6.1k Golang : Measure execution time for a function