重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关Go中基础模板怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联公司长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为长汀企业提供专业的网站制作、成都做网站,长汀网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。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.htmlGo Web Package info:
浏览器访问:http://localhost:4000/
右键查看网页源代码:view-source:http://localhost:4000/
Go Web Package info:
首先完成从请求参数中获取成绩score的大小 vim main.htmlGo Web {{.}} 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.htmlGo Web {{if .}} score is {{.}} {{else}} no score {{end}} 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.htmlGo Web {{if gt . 90}} A {{else if gt . 80}} B {{else if gt . 70}} C {{else if gt . 60}} D {{else}} F {{end}} 报错: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.htmlGo Web {{range $key,$value := .Header}}Method: {{$.Method}} key: {{$key}}
{{range $value}}
http://localhost:4000/
Go Web {{with .URL}} {{.}} {{.Scheme}} {{.User}} {{.Host}} {{.Path}} {{end}} 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
web中想临时输出一些字符串,字段,怎么办?
维护一个大的结构不方便
将跟对象变成一个key类型为string,值类型为空接口的map,再在map中放入需要传入的所有对象,除此之外,还有一个好处,就是可以把map申明为一个变量,在不同的 Handler中进行传递,在最后响应的这个Handler之前,不断的将map中添加新的内容,最终模板渲染的时候,就可以使用到所有的内容
Go Web {{.Request}} {{.Score}} 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中基础模板怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。