C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: Once the Writer is created, we can create a new template with template.New. We provide a name for the template ("example").
Parse: We call this func and pass a specially-formatted string. The substitution comes after the word "Hello" here.
Execute: Here we pass the Writer, followed by the values to be inserted into the HTML template.
Golang program that uses html template
package main
import (
"bufio"
"html/template"
"os"
)
func main() {
// Create a file.
f, _ := os.Create("C:\\programs\\file.txt")
// New writer.
out := bufio.NewWriter(f)
// Create template and parse it.
tmpl, _ := template.New("example").Parse("<p>Hello {{.}}</p>")
// Insert string into substitution.
tmpl.Execute(out, "Friend")
// Done.
out.Flush()
}
Output
<p>Hello Friend</p>