多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 模板引擎的使用 Go语言模板引擎的使用可以分为三部分:**定义模板文件**、**解析模板文件**和**模板渲染.** **定义模板文件** ~~~ <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Hello</title> </head> <body> <p>Hello {{.}}</p> </body> </html> ~~~ **解析和渲染模板文件** ~~~ func sayHello(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseFiles("./hello.tmpl") if err != nil { fmt.Println("create template failed, err: ", err) return } tmpl.Execute(w, "Alice Cooper") } func main() { http.HandleFunc("/", sayHello) err := http.ListenAndServe(":9000", nil) if err != nil { fmt.Println("HTTP server failed, err: ", err) return } } ~~~ **Go模板语法中的条件判断有以下几种:** ~~~template {{if pipeline}} T1 {{end}} {{if pipeline}} T1 {{else}} T0 {{end}} {{if pipeline}} T1 {{else if pipeline}} T0 {{end}} ~~~