gogin框架
1)ginDemo
package main
import (
"github.com/gin-gonic/gin"
)
func sayHello(c *gin.Context) {
c.JSON(200, gin.H{
"msg": "Hello Golang!",
})
}
func main() {
r := gin.Default()
r.GET("/Hello", sayHello)
_ = r.Run(":9090")
}
2)RESTful API接口开发
r.GET("/book", ...)
r.POST("/book", ...)
r.PUT("/book", ...)
r.DELETE("/book", ...)
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/book", func(c *gin.Context){
c.JSON(200, gin.H{
"method" :"GET",
})
})
r.POST("/book", func(c *gin.Context){
c.JSON(200, gin.H{
"method" :"POST",
})
})
r.PUT("/book", func(c *gin.Context){
c.JSON(200, gin.H{
"method" :"PUT",
})
})
r.DELETE("/book", func(c *gin.Context){
c.JSON(200, gin.H{
"method" :"DELETE",
})
})
_ = r.Run(":9090")
}
3)HTML模板渲染
传个单对象,也可以传多个对象
模板大多已.tml结尾,必须utf8编码
package main
import (
"fmt"
"html/template"
"net/http"
)
type User struct {
Name string
Gender string
Age int
}
func main() {
u1 := User{
Name: "张三",
Gender: "男",
Age: 13,
}
http.HandleFunc("/index",func(w http.ResponseWriter, r *http.Request){
t, err := template.ParseFiles("hello.tmpl")
if err != nil {
fmt.Println("解析模板错误")
return
}
_ = t.Execute(w ,u1)
})
_ = http.ListenAndServe(":80",nil)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<p style="color:red">姓名 {{ .u1.Name }}</p>
<p style="color:red">性别 {{ .u1.Gender }}</p>
<p style="color:red">年龄 {{ .u1.Age }}</p>
<p style="color:red">姓名 {{ .map.name }}</p>
<p style="color:red">性别 {{ .map.gender }}</p>
<p style="color:red">年龄 {{ .map.age }}</p>
模板判断
<hr>
{{ $v1 := 100 }} <!-- 模板中定义变量 -->
{{ $age := .m1.age }} <!-- 模板中定义变量 -->
{{ if $v1 }}
这里有值
{{ else }}
啥都没有
{{ end }}
比较运算
<hr>
{{ if lt .u1.Age 22 }}
好好上学
{{ else }}
好好工作
{{ end }}
<br>
遍历
<hr>
{{ range $id, $hobby := .map.hobby }}
{{ $id }} - {{ $hobby }}<br>
{{ end }}
<hr>
{{ with .map }} <!-- with .map 等同于 var . = .map -->
{{ .name }}
{{ .gender }}
{{ .age }}
{{end}}
{{index .map.hobby 1 }}
</body>
</html>
4)自定义模板函数
package main
import(
"fmt"
"html/template"
"net/http"
)
func testTemplate(w http.ResponseWriter, r *http.Request){
kua := func(name string) (string, error) {
return name + "年轻帅气", nil
}
t := template.New("f.tmpl")
t.Funcs(template.FuncMap{
"kua": kua,
})
_,err:=t.ParseFiles("f.tmpl")
if err != nil {
fmt.Println("解析失败")
return
}
name:= "小王子"
t.Execute(w,name)
}
func main(){
http.HandleFunc("/",testTemplate)
fmt.Println("web服务已启动")
http.ListenAndServe(":8080", nil)
}
5)模板嵌套
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>嵌套模板</title>
</head>
<body>
<h1>嵌套模板</h1>
<hr>
{{}}
{{template "ul.tmpl"}}
<hr>
{{}}
{{template "ol.tmpl"}}
<div>你好 {{.}}</div>
</body>
</html>
{{}}
{{define "ol.tmpl"}}
我是ol模板下的内容
<ol>
<li>吃饭</li>
<li>睡觉</li>
<li>打豆豆</li>
</ol>
{{end}}
6)继承模板
package main
import (
"fmt"
"html/template"
"net/http"
)
func main() {
http.HandleFunc("/",func(w http.ResponseWriter, r *http.Request){
t,err := template.ParseFiles("template/mu.tmpl")
if err != nil {
fmt.Println("ParentFiles is not error")
}
t.Execute(w,"小王子")
})
http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("template/mu.tmpl", "template/index.tmpl")
_ = t.ExecuteTemplate(w, "index.tmpl", "小王子")
})
http.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("template/mu.tmpl", "template/home.tmpl")
_ = t.ExecuteTemplate(w, "home.tmpl", "小王子")
})
_ = http.ListenAndServe(":8080", nil)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板继承</title>
<style>
div {
width: 100%;
color: bisque;
height: 13px;
}
</style>
</head>
<body>
<div>
我是mu模板下的内容
</div>
{{block "content" .}}{{end}}
</body>
</html>
{{template "mu.tmpl" .}}
{{define "content"}}
<h1>我是index文本</h1>
<p>Hello {{.}}</p>
{{end}}
7)gin模板渲染
package main
import (
"github.com/gin-gonic/gin"
"html/template"
"net/http"
)
func main() {
r := gin.Default()
r.SetFuncMap(template.FuncMap{
"safe" : func(str string) template.HTML {
return template.HTML(str)
},
})
r.Static("/xxx","templates/statics")
r.LoadHTMLGlob("templates/**/*")
r.GET("/posts",func(c *gin.Context){
c.HTML(http.StatusOK,"posts/index.tmpl",gin.H{
"title": "<a href=\"http://www.liwenzhou.com\">李文周的博客</a>",
})
})
r.GET("/users",func(c *gin.Context){
c.HTML(http.StatusOK,"users/index.tmpl",gin.H{
"title": "liwenzhou.com",
})
})
_ = r.Run(":8080")
}
8)gin模板响应json数据
package mainimport "C"import ( "github.com/gin-gonic/gin" "net/http")
9)gin 获取请求参数
package mainimport ( "github.com/gin-gonic/gin" "net/http")func main() { r := gin.Default()