Standard library / text/template
text/template.Template.ParseFiles()
Parses files into the receiver namespace; use the basename to select the definition. Expected stdout: 'Hello Ada\n'.
Formal Syntax & Structure
go syntaxgo
func (t *Template) ParseFiles(filenames ...string) (*Template, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| filenames | ...string | Paths of template files to parse. |
Return Value
(*Template, error)
Parses files into the receiver namespace; use the basename to select the definition.
Working Example
Example Snippetgo
package main
import ( "fmt" "os" "path/filepath" "text/template")
func check(e error) { if e != nil { panic(e) }}
func main() { dir, e := os.MkdirTemp("", "template-demo-") check(e) defer os.RemoveAll(dir) path := filepath.Join(dir, "greet.tmpl") check(os.WriteFile(path, []byte("Hello {{.}}"), 0600)) t, e := template.New("greet.tmpl").ParseFiles(path) check(e) check(t.Execute(os.Stdout, "Ada")) fmt.Println()}