概述
-
-
- 模板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 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:golanggo_prosrcsafly> 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, 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:golanggo_prosrcsafly> 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, 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")
}
}
最后
以上就是自由乌冬面为你收集整理的golang基础-模板if判断、传(map_arr切片)数据渲染浏览器的全部内容,希望文章能够帮你解决golang基础-模板if判断、传(map_arr切片)数据渲染浏览器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复