一、

1.安装

go get -u github.com/gin-gonic/gin

2.例子

package main

import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(, gin.H{
"message": "pong",
})
})
r.Run()
}

3.测试

http://127.0.0.1:8080/ping

4.结合 vue和Bootstrap

main.go

package main

import (
"net/http" "github.com/gin-gonic/gin"
) func main() {
r := gin.Default()
r.LoadHTMLGlob("view/*")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{})
})
r.Run()
}

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
</head>
<body>
<div id="vm" class="container">
<div class="d-flex justify-content-center p-5">
<input v-model="message"/>
</div>
<div class="d-flex justify-content-center">
<h1>${message}</h1>
</div>
</div> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" ></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var vm = new Vue({
// 由于双花括号和go模板冲突,把vue双花改成${message}形式
delimiters: ['${', '}'],
el: '#vm',
data: {
message: null
}
})
</script>
</body>
</html>

二、

1.请求方式及请求路径

func main() {
//使用默认中间件创建一个gin路由器
r := gin.Default()
//get请求方式:参数1地址,参数2函数
r.GET("/", getting)
//启动默认端口8080
r.Run()
} func getting(c *gin.Context) {
//
c.JSON(, "Hello")
}

2.参数

(1)路径传参

127.0.0.1:8080/tom

func main() {
r := gin.Default()
//get请求方式:参数1地址,参数2函数
r.GET("/:name", getting)
r.Run()
} func getting(c *gin.Context) {
//
name := c.Param("name")
c.String(, "Hello %s", name)
}

(2)get

func main() {
r := gin.Default()
r.GET("/", getting)
r.Run()
} func getting(c *gin.Context) {
//设置默认值,如果客户端为传递str1,则默认赋值为def
str1 := c.DefaultQuery("str1", "def")
str2 := c.Query("str2") // 是 c.Request.URL.Query().Get("str2") 的简写
c.String(, "Hello %s\n", str1)
c.String(, "Hello %s", str2)
}

(3)表单

func main() {
r := gin.Default()
r.POST("/", posting)
r.Run()
} func posting(c *gin.Context) { name := c.PostForm("name")
c.String(, "Hello %s \n", name) age := c.DefaultPostForm("age", "") // 此方法可以设置默认值
c.String(, "Hello %s", age)
}

最新文章

  1. Android—android与js交互以及相互传参
  2. ik扩展支持Solr配置
  3. lca入门———树上倍增法(博文内含例题)
  4. ASP.NET程序单客户端(浏览器)登录的实现方案
  5. json_encode和json_decode
  6. No connection string named &#39;***&#39; could be found in the application config file
  7. oracle表相关
  8. WebLogic Exception
  9. UVA 11133 - Eigensequence DP
  10. ocx在我indows7无法注册
  11. XML学习笔记(2)--dom4j操作XML
  12. 【HDOJ】4729 An Easy Problem for Elfness
  13. ubuntu下virtualbox使用u盘
  14. 生成四位随机数的PHP代码
  15. PHP自学3——在html的&lt;table&gt;标签中显示用户提交表单
  16. C++矩阵处理库--Eigen初步使用
  17. PHP基础入门(三)---PHP函数基础
  18. 实验与作业(Python)-03 Python程序实例解析
  19. dat.gui stats.js 通用参数配置及图像统计工具
  20. 1、Linux文件结构介绍

热门文章

  1. jmeter之-非GUI模式&amp;登录实战
  2. [CSP-S模拟测试45]题解
  3. (转)使用OpenGL显示图像(五)添加移动
  4. Java核心基础知识(一):概念、语法、使用、源码
  5. java连SQLServer失败 java.lang.ClassNotFoundException:以及 javax.xml.bind.JAXBException
  6. JS 多个条件判断
  7. Jackson教程
  8. Android Studio3.3中Cannot resolve symbol ActivityTestRule
  9. Windows7下移植Qt4.8.4项目到QT5.2上时遇到的一些问题
  10. Spring JAR下载地址