Created
February 21, 2020 01:33
-
-
Save wangsongyan/f9e187b8f5a7933d5c61d0dab68c56db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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