您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关Go中基础模板怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
package main import( "html/template" "log" "os" ) func main(){ tmpl,err := template.New("go-web").Parse(`Hello World!`)//先创建一个template对象 if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印错误 } err = tmpl.Execute(os.Stdout,nil) //调用方法Execute,两个参数:目的地,data先等于nil if err != nil{ log.Fatal("Execute: %v", err) } } go run test.go 输出: Hello World!
package main import( "html/template" "log" "os" ) func main(){ tmpl,err := template.New("go-web").Parse(`Hello World!,{{.}}`)//先创建能够根据输入数据的不同,产生不同的效果的动态模板{{}}, “.”代表跟对象“go web” if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印错误 } err = tmpl.Execute(os.Stdout,"go web") //调用方法Execute,两个参数:目的地,data先等于go web,此时跟对象的值是 go web if err != nil{ log.Fatal("Execute: %v", err) } } go run test.go 输出: Hello World!,go web package main import( "html/template" "log" "os" ) type Package struct {//自定义的传入类型Package Name string NumFuncs int NumVars int } func main(){ tmpl,err := template.New("go-web").Parse(`Hello World!,{{.}}`)//先创建能够根据输入数据的不同,产生不同的效果的动态模板{{}}, .代表跟对象 if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印错误 } err = tmpl.Execute(os.Stdout,&Package{ //自定义的传入类型Package Name :"go-web", NumFuncs:12, NumVars:1200, }) //调用方法Execute,两个参数:目的地,data先等于go web,此时跟对象的值是 go web if err != nil{ log.Fatal("Execute: %v", err) } } go run test.go 输出: Hello World!,{go-web 12 1200} 如何将模板渲染的结果输出到stdout呢? package main import( "html/template" "log" "os" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ tmpl,err := template.New("go-web").Parse(` Package name: {{.Name}} //通过反射寻找方法,字段或者函数,Name必须公开,必须首字母大写, Number of functions: {{.NumFuncs}} Number of variables: {{.NumVars}} `)//先创建能够根据输入数据的不同,产生不同的效果的动态模板{{}}, .代表跟对象 if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印错误 } err = tmpl.Execute(os.Stdout,&Package{ Name :"go-web", NumFuncs:12, NumVars:1200, }) //调用方法Execute,两个参数:目的地,data先等于go web,此时跟对象的值是 go web if err != nil{ log.Fatal("Execute: %v", err) } } go run test.go 输出: Package name: go-web Number of functions: 12 Number of variables: 1200
package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.New("go-web").Parse(` Package name: {{.Name}} Number of functions: {{.NumFuncs}} Number of variables: {{.NumVars}} `)//先创建能够根据输入数据的不同,产生不同的效果的动态模板{{}}, .代表跟对象 if err != nil { fmt.Fprintf(writer,"Parse:%v",err) return } err = tmpl.Execute(writer,&Package{ Name :"go-web", NumFuncs:12, NumVars:1200, }) //调用方法Execute,两个参数:目的地,data先等于go web,此时跟对象的值是 go web if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 10:38:40 Starting Server....
浏览器访问:http://localhost:4000/
package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.tmpl") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } err = tmpl.Execute(writer,&Package{ Name :"go-web", NumFuncs:14, NumVars:1200, }) //调用方法Execute,两个参数:目的地,data先等于go web,此时跟对象的值是 go web if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } 然后编辑本地main.tmpl文件: ➜ test pwd /Users/daixuan/qbox/test ➜ test vim main.tmpl Package name: {{.Name}} Number of functions: {{.NumFuncs}} Number of variables: {{.NumVars}} go run test.go 输出: 2018/07/10 10:52:56 Starting Server....
浏览器访问:http://localhost:4000/
➜ test vim main.tmpl Method: {{.Method}} package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.tmpl") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } err = tmpl.Execute(writer,request) //调用方法Execute,两个参数:目的地,data先等于go web,此时跟对象的值是 go web if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 11:01:28 Starting Server....
浏览器访问:http://localhost:4000/ 打印出了Method:GET
修改main.tmpl Method: {{.Method}} URL: {{.URL.Path}}
浏览器访问:http://localhost:4000/ 打印出Path: /
打印User-Agent
修改main.tmpl Method: {{.Method}} Path: {{.URL.Path}} Header: {{.Header.Get "User-Agent"}} package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.tmpl") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } err = tmpl.Execute(writer,request) //调用方法Execute,两个参数:目的地,data先等于go web,此时跟对象的值是 go web if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 11:10:01 Starting Server....
浏览器访问:http://localhost:4000/
vim main.html <html> <head> <title>Go Web</title> </head> <body> <p>Package info:</p> <li>Package name: {{.Name}}}</li> <li>Number of functions:{{.NumFuncs}}}</li> <li>Number of variables: {{.NumVars}}}</li> </body> </html> package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.html") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } err = tmpl.Execute(writer,&Package{ Name :"go-web", NumFuncs:14, NumVars:1200, }) if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 11:24:51 Starting Server....
浏览器访问:http://localhost:4000/
右键查看网页源代码:view-source:http://localhost:4000/
<html> <head> <title>Go Web</title> </head> <body> <p>Package info:</p> <li>Package name: go-web}</li> <li>Number of functions:14}</li> <li>Number of variables: 1200}</li> </body> </html>
首先完成从请求参数中获取成绩score的大小 vim main.html <html> <head> <title>Go Web</title> </head> <body> {{.}} </body> </html> package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.html") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } score := request.FormValue("score") err = tmpl.Execute(writer,score) if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 11:33:01 Starting Server....
访问:http://localhost:4000/?score=100
输出100
访问:http://localhost:4000/?score=1
输出1
符合预期
判断score是否存在
vim main.html <html> <head> <title>Go Web</title> </head> <body> {{if .}} score is {{.}} {{else}} no score {{end}} </body> </html> vim test.go package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.html") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } score := request.FormValue("score") err = tmpl.Execute(writer,score) if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 11:38:10 Starting Server....
访问:http://localhost:4000/?score=100
访问:http://localhost:4000/
对数值进行比较
vim main.html <html> <head> <title>Go Web</title> </head> <body> {{if gt . 90}} A {{else if gt . 80}} B {{else if gt . 70}} C {{else if gt . 60}} D {{else}} F {{end}} </body> </html> 报错:Execute:template: main.html:6:13: executing "main.html" at : error calling gt: incompatible types for comparison 原因是request.FormValue("score")返回的是string类型,不能与int型90比较 修改返回类型 vim test.go package main import( "html/template" "log" "net/http" "fmt" "strconv" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.html") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } score := request.FormValue("score") num, _ := strconv.Atoi(score) err = tmpl.Execute(writer,num) if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 11:53:59 Starting Server....
访问:http://localhost:4000/?score=100 输出A
http://localhost:4000/?score=80 输出C
http://localhost:4000/?score=60 输出F
vim main.html <html> <head> <title>Go Web</title> </head> <body> {{range $key,$value := .Header}} <p> Method: {{$.Method}} key: {{$key}} </p> <p> {{range $value}} <li>{{.}}</li> {{end}} </p> {{end}} </body> </html> vim test.go package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.html") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } err = tmpl.Execute(writer,request) if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 12:14:13 Starting Server....
http://localhost:4000/
<html> <head> <title>Go Web</title> </head> <body> {{with .URL}} {{.}} {{.Scheme}} {{.User}} {{.Host}} {{.Path}} {{end}} </body> </html> package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.html") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } err = tmpl.Execute(writer,request) if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出:
访问:http://localhost:4000/1234?score=80 输出:
/1234?score=80 <nil> /1234
web中想临时输出一些字符串,字段,怎么办?
维护一个大的结构不方便
将跟对象变成一个key类型为string,值类型为空接口的map,再在map中放入需要传入的所有对象,除此之外,还有一个好处,就是可以把map申明为一个变量,在不同的 Handler中进行传递,在最后响应的这个Handler之前,不断的将map中添加新的内容,最终模板渲染的时候,就可以使用到所有的内容
<html> <head> <title>Go Web</title> </head> <body> {{.Request}} {{.Score}} </body> </html> package main import( "html/template" "log" "net/http" "fmt" ) type Package struct { Name string NumFuncs int NumVars int } func main(){ http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { tmpl,err := template.ParseFiles("main.html") if err != nil { fmt.Fprintf(writer,"ParseFiles:%v",err) return } err = tmpl.Execute(writer,map[string]interface{}{ "Request": request, "Score": 97, }) if err != nil{ fmt.Fprintf(writer,"Execute:%v",err) return } }) log.Println("Starting Server....") log.Fatal(http.ListenAndServe(":4000",nil)) } go run test.go 输出: 2018/07/10 12:29:10 Starting Server....
http://localhost:4000/ 输出score=97 和GET方法
关于“Go中基础模板怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。