Skip to content

Instantly share code, notes, and snippets.

@wangsongyan
Created February 21, 2020 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangsongyan/f9e187b8f5a7933d5c61d0dab68c56db to your computer and use it in GitHub Desktop.
Save wangsongyan/f9e187b8f5a7933d5c61d0dab68c56db to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"html/template"
"log"
)
type Instance struct {
Name string
Age int
Gender string
}
func main() {
ins := Instance{
Name: "v2ex",
}
log.Println(Render_("http.tmpl", "templates/http.tmpl", ins))
}
func Render_(tmpName, tmp string, ins Instance) []byte {
// tmp 为已定义模板的路径 如 "templates/http.tmpl"
tpl, err := template.ParseFiles(tmp)
if err != nil {
log.Fatal(err)
}
data := ins
var buf bytes.Buffer
if err := tpl.ExecuteTemplate(&buf, tmpName, data); err != nil {
log.Fatal(err)
}
return []byte(buf.String())
//fmt.Println(buf.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment