您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。