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

前端之家收集整理的这篇文章主要介绍了golang基础-模板if判断、传(map_arr切片)数据渲染浏览器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

模板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 main

import (
    // "os"
    "fmt"
    "html/template"
    "io"
    "net/http"
)

var myTemplate *template.Template

type 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.go
called by template
render 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 main

import (
    // "os"
    "fmt"
    "html/template"
    "io"
    "net/http"
)

var myTemplate *template.Template

type Person struct {
    Name string
    Age  int
}

type Result struct {
    output string
}
/* 自定义实现接口 type Writer interface { Write(p []byte) (n int,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,nil)
    if err != nil {
        fmt.Println("http listen Failed")
    }
}

index.html:

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

浏览器输入

终端输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
called by template
render data: hello world
called by template
render 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 main

import (
    // "os"
    "fmt"
    "html/template"
    "io"
    "net/http"
)

var myTemplate *template.Template

type Person struct {
    Name string
    Age  int
    Title string
}

type Result struct {
    output string
}
/* 自定义实现接口 type Writer interface { Write(p []byte) (n int,r *http.Request) {
    var arr []Person
    p := Person{Name: "Mary001",Age: 10,Title: "我的个人网站"}
    p1 := Person{Name: "Mary002",Title: "我的个人网站"}
    p2 := Person{Name: "Mary003",Title: "我的个人网站"}
    arr = append(arr,p)
    arr = append(arr,p1)
    arr = append(arr,p2)



    resultWriter := &Result{}
    io.WriteString(resultWriter,arr)
    fmt.Println("render data:",nil)
    if err != nil {
        fmt.Println("http listen Failed")
    }
}

猜你在找的Go相关文章