暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Gin简介

一纸旧时光 2021-01-23
608

Gin[1]简介与环境部署

Gin 是Golang[2]的 Web 框架。

user@Debian:~$ wget https://studygolang.com/dl/golang/go1.15.7.linux-amd64.tar.gz
user@Debian:~$ tar -zxvf go1.15.7.linux-amd64.tar.gz -C /opt
user@Debian:~$ sudo nano ~/.bashrc
export GOROOT=/opt/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
user@Debian:~$ source ~/.bashrc

user@Debian:~$ go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,https://goproxy.cn,https://goproxy.io,direct # 换国内源

user@Debian:~$ mkdir GinProject
user@Debian:~/GinProject$ go mod init GinProject # 生成包管理文件

user@Debian:~/GinProject$ export http_proxy="127.0.0.1:1080"export https_proxy="127.0.0.1:1080" # 开代理
user@Debian:~/GinProject$ go get -u -v github.com/gin-gonic/gin # -u是下载并安装代码包,不管工作区中是否存在,-v是显示过程

user@Debian:~/GinProject$ go get -v -u github.com/pilu/fresh
user@Debian:~/GinProject$ fresh # fresh替代go run main.go可以支持热更新

路由

package main // 标识同级包

// 导入包
import (
 "net/http"
 "github.com/gin-gonic/gin"
)

// 主程序入口
func main() {
 r := gin.Default() // 应用实例

 redirect(r)
 group(r)

 r.Run(":9999"// 指定端口运行应用
}

// 函数
func redirect(r *gin.Engine) {
 // 重定向
 r.GET("/"func(c *gin.Context) {
  c.Request.URL.Path = "/redirect"
  r.HandleContext(c)
 })
 r.GET("/redirect"func(c *gin.Context) {
  c.Redirect(http.StatusMovedPermanently, "https://baidu.com")
 })
}

func group(r *gin.Engine) {
 api := r.Group("/api"// 分组路由
 {
  api.GET("/:name"func(c *gin.Context) {
   name := c.Param("name"// 地址参数

   age := c.Query("age")                 // 请求参数
   role := c.DefaultQuery("role""job"// 默认请求参数

   id := c.QueryMap("id"// 请求字典

   c.JSON(http.StatusOK, gin.H{
    "name": name,
    "age":  age,
    "role": role,
    "id":   id,
   })
  })
  api.POST("/post/*optional"func(c *gin.Context) {
   nums := c.PostFormMap("nums"// Post表单字典

   username := c.PostForm("username")             // Post表单参数
   passwd := c.DefaultPostForm("password""123"// 默认Post参数

   c.JSON(http.StatusOK, gin.H{
    "nums":     nums,
    "username": username,
    "password": passwd,
   })
  })
 }
}

Q:路由方法有哪几种?
A:路由方法有GET
POST
PUT
PATCH
DELETE
OPTIONS
Any

user@Debian:~$ curl -g "http://localhost:8080/api/Tom?age=18&role=worker&id[one]=1&id[two]=2" # curl是个客户端请求服务器的命令行工具;-g是构造请求字符串
user@Debian:~$ curl -g "http://localhost:8080/api/post/" -X POST -d "nums[one]=1&nums[two]=2&username=Tom&password=123" # -X是设置请求方法,-d是发送POST请求体

模板

package main

import (
 "net/http"
 "github.com/gin-gonic/gin"
)

func main() {
 r := gin.Default()

 tmpl(r)

 r.Run()
}

// 结构体
type person struct {
 Name string
 Age  int8
}

func tmpl(r *gin.Engine) {
 r.LoadHTMLGlob("templates/**")
 p := &person{
  Name: "John",
  Age:  18,
 }

 r.GET("/tmpl"func(c *gin.Context) {
  c.HTML(http.StatusOK, "index.tmpl", gin.H{
   "title""Gin",
   "arr":   [1]*person{p},
  })
 })
}

选择判断

{{if .cond1}}
{{else if .cond2}}
{{ else }}
{{ end }}

{{if not .cond}}
{{ end }}

{{if and .cond1 .cond2}}
{{ end }}

{{if or .cond1 .cond2}}
{{ end }}

{{if eq .cond1 .cond2}}
{{ end }}

{{if ne .cond1 .cond2}}
{{ end }}

{{if lt .cond1 .cond2}}
{{ end }}

{{if le .cond1 .cond1}}
{{ end }}

{{if gt .cond1 .cond1}}
{{ end }}

{{if ge .cond1 .cond2}}
{{ end }}

循环

{{range $index,$ele:=.arr}}
<p>{{ $index }}:{{ $ele.Name }}{{ $ele.Age }}</p>
{{ end }}

{{range .arr}}
<!-- 访问内部 -->
<p>{{ .innerVal }}</p>

<!-- 访问外部 -->
<p>{{ $.val }}</p>
{{ end }}

嵌套

<!-- 定义子模板 -->
{{define "sub"}}
{{ end }}

<!-- 引入子模板,并传递变量 -->
{{template "sub" .}}

? Ask More...

[1]

Gin: https://gin-gonic.com/zh-cn/

[2]

Golang: https://golang.google.cn/

- END -


文章转载自一纸旧时光,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论