Golang output templates

How to display the contents of the template?

main package

import (
    "fmt"
    "html/template"
    "os"

)

func main() {
    t := template.New("another")
    t,e:=t.ParseFiles("test.html")
    if(e!=nil){
            fmt.Println(e);
    }
    t.Execute(os.Stdout, nil)

}

Why not? There is test.html

+5
source share
1 answer

You do not need to create a new template with Newand then use ParseFiles. There is also a feature ParseFilesthat takes care of creating a new template backstage.
Here is an example:

package main

import (
    "fmt"
    "html/template"
    "os"
)

func main() {
    t, err := template.ParseFiles("test.html")
    if err != nil {
            fmt.Println(err);
    }
    t.Execute(os.Stdout, nil)
}
+7
source

All Articles