您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Go中基礎(chǔ)模板怎么用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
package main import( "html/template" "log" "os" ) func main(){ tmpl,err := template.New("go-web").Parse(`Hello World!`)//先創(chuàng)建一個(gè)template對(duì)象 if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印錯(cuò)誤 } err = tmpl.Execute(os.Stdout,nil) //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,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!,{{.}}`)//先創(chuàng)建能夠根據(jù)輸入數(shù)據(jù)的不同,產(chǎn)生不同的效果的動(dòng)態(tài)模板{{}}, “.”代表跟對(duì)象“go web” if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印錯(cuò)誤 } err = tmpl.Execute(os.Stdout,"go web") //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,data先等于go web,此時(shí)跟對(duì)象的值是 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!,{{.}}`)//先創(chuàng)建能夠根據(jù)輸入數(shù)據(jù)的不同,產(chǎn)生不同的效果的動(dòng)態(tài)模板{{}}, .代表跟對(duì)象 if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印錯(cuò)誤 } err = tmpl.Execute(os.Stdout,&Package{ //自定義的傳入類型Package Name :"go-web", NumFuncs:12, NumVars:1200, }) //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,data先等于go web,此時(shí)跟對(duì)象的值是 go web if err != nil{ log.Fatal("Execute: %v", err) } } go run test.go 輸出: Hello World!,{go-web 12 1200} 如何將模板渲染的結(jié)果輸出到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}} //通過(guò)反射尋找方法,字段或者函數(shù),Name必須公開,必須首字母大寫, Number of functions: {{.NumFuncs}} Number of variables: {{.NumVars}} `)//先創(chuàng)建能夠根據(jù)輸入數(shù)據(jù)的不同,產(chǎn)生不同的效果的動(dòng)態(tài)模板{{}}, .代表跟對(duì)象 if err != nil { log.Fatalf("Parse:%v",err)//fatalf格式化字符串,打印錯(cuò)誤 } err = tmpl.Execute(os.Stdout,&Package{ Name :"go-web", NumFuncs:12, NumVars:1200, }) //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,data先等于go web,此時(shí)跟對(duì)象的值是 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}} `)//先創(chuàng)建能夠根據(jù)輸入數(shù)據(jù)的不同,產(chǎn)生不同的效果的動(dòng)態(tài)模板{{}}, .代表跟對(duì)象 if err != nil { fmt.Fprintf(writer,"Parse:%v",err) return } err = tmpl.Execute(writer,&Package{ Name :"go-web", NumFuncs:12, NumVars:1200, }) //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,data先等于go web,此時(shí)跟對(duì)象的值是 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....
瀏覽器訪問(wèn):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, }) //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,data先等于go web,此時(shí)跟對(duì)象的值是 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....
瀏覽器訪問(wèn):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) //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,data先等于go web,此時(shí)跟對(duì)象的值是 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....
瀏覽器訪問(wèn):http://localhost:4000/ 打印出了Method:GET
修改main.tmpl Method: {{.Method}} URL: {{.URL.Path}}
瀏覽器訪問(wèn):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) //調(diào)用方法Execute,兩個(gè)參數(shù):目的地,data先等于go web,此時(shí)跟對(duì)象的值是 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....
瀏覽器訪問(wèn):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....
瀏覽器訪問(wèn):http://localhost:4000/
右鍵查看網(wǎng)頁(yè)源代碼: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>
首先完成從請(qǐng)求參數(shù)中獲取成績(jī)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....
訪問(wèn):http://localhost:4000/?score=100
輸出100
訪問(wèn):http://localhost:4000/?score=1
輸出1
符合預(yù)期
判斷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....
訪問(wèn):http://localhost:4000/?score=100
訪問(wèn):http://localhost:4000/
對(duì)數(shù)值進(jìn)行比較
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> 報(bào)錯(cuò):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....
訪問(wèn):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 輸出:
訪問(wèn):http://localhost:4000/1234?score=80 輸出:
/1234?score=80 <nil> /1234
web中想臨時(shí)輸出一些字符串,字段,怎么辦?
維護(hù)一個(gè)大的結(jié)構(gòu)不方便
將跟對(duì)象變成一個(gè)key類型為string,值類型為空接口的map,再在map中放入需要傳入的所有對(duì)象,除此之外,還有一個(gè)好處,就是可以把map申明為一個(gè)變量,在不同的 Handler中進(jìn)行傳遞,在最后響應(yīng)的這個(gè)Handler之前,不斷的將map中添加新的內(nèi)容,最終模板渲染的時(shí)候,就可以使用到所有的內(nèi)容
<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方法
關(guān)于“Go中基礎(chǔ)模板怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。