golang基础-模板if判断、传(map_arr切片)数据渲染浏览器

来源:互联网 发布:互联网之子 知乎 编辑:程序博客网 时间:2024/06/16 11:28

      • 模板if判断
      • 模板if常见操作符
      • 传map数据渲染浏览器
      • 传map数据渲染浏览器

模板if判断

index.html

<html>    <head>    </head>    <body>            {{if gt .Age 18}}            <p>hello, old man, {{.Name}}</p>            {{else}}            <p>hello,young man, {{.Name}}</p>            {{end}}    </body></html>

demo.go

package mainimport (    // "os"    "fmt"    "html/template"    "io"    "net/http")var myTemplate *template.Templatetype Person struct {    Name string    Age  int}type Result struct {    output string}/*自定义实现接口type Writer interface {    Write(p []byte) (n int, err error)}*/func (p *Result) Write(b []byte) (n int, err error) {    fmt.Println("called by template")    p.output += string(b)    return len(b), nil}/*func WriteString(w Writer, s string) (n int, err error) {    if sw, ok := w.(stringWriter); ok {        return sw.WriteString(s)    }    return w.Write([]byte(s))}*/func userInfo(w http.ResponseWriter,r *http.Request) {    p := Person{Name:"safly",Age:30}    resultWriter := &Result{}    io.WriteString(resultWriter, "hello world")    myTemplate.Execute(w,p)    fmt.Println("render data:",resultWriter.output)}func initTemplate(fileName string) (err error){    myTemplate,err  = template.ParseFiles(fileName)    if err != nil{        fmt.Println("parse file err:",err)        return    }    return}/*func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {    DefaultServeMux.HandleFunc(pattern, handler)}*/func main() {    initTemplate("e:/golang/go_pro/src/safly/index.html")    http.HandleFunc("/user/info", userInfo)    err := http.ListenAndServe("0.0.0.0:8880", nil)    if err != nil {        fmt.Println("http listen failed")    }}

浏览器输入
这里写图片描述
终端输出如下:

PS E:\golang\go_pro\src\safly> go run demo.gocalled by templaterender data: hello world

模板if常见操作符

•   not 非{{if not .condition}} {{end}}•   and 与{{if and .condition1 .condition2}} {{end}}•   or 或{{if or .condition1 .condition2}} {{end}}•   eq 等于{{if eq .var1 .var2}} {{end}}•   ne 不等于{{if ne .var1 .var2}} {{end}}•   lt 小于 (less than){{if lt .var1 .var2}} {{end}}•   le 小于等于{{if le .var1 .var2}} {{end}}•   gt 大于{{if gt .var1 .var2}} {{end}}•   ge 大于等于{{if ge .var1 .var2}} {{end}}

传map数据渲染浏览器

package mainimport (    // "os"    "fmt"    "html/template"    "io"    "net/http")var myTemplate *template.Templatetype Person struct {    Name string    Age  int}type Result struct {    output string}/*自定义实现接口type Writer interface {    Write(p []byte) (n int, err error)}*/func (p *Result) Write(b []byte) (n int, err error) {    fmt.Println("called by template")    p.output += string(b)    return len(b), nil}/*func WriteString(w Writer, s string) (n int, err error) {    if sw, ok := w.(stringWriter); ok {        return sw.WriteString(s)    }    return w.Write([]byte(s))}*/func userInfo(w http.ResponseWriter,r *http.Request) {    // p := Person{Name:"safly",Age:30}    p:= make(map[string]interface{})    p["Name"] = "safly"    p["Age"] = 18    resultWriter := &Result{}    io.WriteString(resultWriter, "hello world")    myTemplate.Execute(w,p)    fmt.Println("render data:",resultWriter.output)}func initTemplate(fileName string) (err error){    myTemplate,err  = template.ParseFiles(fileName)    if err != nil{        fmt.Println("parse file err:",err)        return    }    return}/*func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {    DefaultServeMux.HandleFunc(pattern, handler)}*/func main() {    initTemplate("e:/golang/go_pro/src/safly/index.html")    http.HandleFunc("/user/info", userInfo)    err := http.ListenAndServe("0.0.0.0:8880", nil)    if err != nil {        fmt.Println("http listen failed")    }}

index.html:

<html>    <head>    </head>    <body>            {{if gt .Age 18}}            <p>hello, old man, {{.Name}}</p>            {{else}}            <p>hello,young man, {{.Name}}</p>            {{end}}            {{.}}    </body></html>

浏览器输入
这里写图片描述
这里写图片描述

终端输出如下:

PS E:\golang\go_pro\src\safly> go run demo.gocalled by templaterender data: hello worldcalled by templaterender data: hello world

传map数据渲染浏览器

index.html

<html>    <head>    </head>    <body>        <p>hello world</p>        <table border="1">        {{range .}}            <tr>                <td>{{.Name}}</td> <td>{{.Age}}</td><td>{{.Title}}</td>            </tr>         {{end}}        </table>    </body>    </html>

demo.go

package mainimport (    // "os"    "fmt"    "html/template"    "io"    "net/http")var myTemplate *template.Templatetype Person struct {    Name string    Age  int    Title string}type Result struct {    output string}/*自定义实现接口type Writer interface {    Write(p []byte) (n int, err error)}*/func (p *Result) Write(b []byte) (n int, err error) {    fmt.Println("called by template")    p.output += string(b)    return len(b), nil}/*func WriteString(w Writer, s string) (n int, err error) {    if sw, ok := w.(stringWriter); ok {        return sw.WriteString(s)    }    return w.Write([]byte(s))}*/func userInfo(w http.ResponseWriter,r *http.Request) {    var arr []Person    p := Person{Name: "Mary001", Age: 10, Title: "我的个人网站"}    p1 := Person{Name: "Mary002", Age: 10, Title: "我的个人网站"}    p2 := Person{Name: "Mary003", Age: 10, Title: "我的个人网站"}    arr = append(arr, p)    arr = append(arr, p1)    arr = append(arr, p2)    resultWriter := &Result{}    io.WriteString(resultWriter, "hello world")    myTemplate.Execute(w,arr)    fmt.Println("render data:",resultWriter.output)}func initTemplate(fileName string) (err error){    myTemplate,err  = template.ParseFiles(fileName)    if err != nil{        fmt.Println("parse file err:",err)        return    }    return}/*func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {    DefaultServeMux.HandleFunc(pattern, handler)}*/func main() {    initTemplate("e:/golang/go_pro/src/safly/index.html")    http.HandleFunc("/user/info", userInfo)    err := http.ListenAndServe("0.0.0.0:8880", nil)    if err != nil {        fmt.Println("http listen failed")    }}

这里写图片描述

原创粉丝点击