提交 d8b23bd7 编写于 作者: 彭世瑜's avatar 彭世瑜

fix

上级 510be4a1
# Go语言Web框架之gin
测试完整代码
https://github.com/mouday/gin-demo
Go语言Web框架:
- [gin](https://gin-gonic.com/zh-cn/)
- beego
- iris
下载安装
```bash
go get -u github.com/gin-gonic/gin
# 等价于
git clone https://github.com/gin-gonic/gin.git
```
初始化项目
```bash
go mod init com/app
```
## 返回json数据
```go
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
// 创建一个服务
server := gin.Default()
// 处理请求
server.GET("/json", func(ctx *gin.Context) {
// 返回json数据
ctx.JSON(200, gin.H{"msg": "Hello"})
})
// 启动服务
server.Run(":8000")
}
```
## 使用中间件
```go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
)
func main() {
// 创建一个服务
server := gin.Default()
// set favicon middleware
server.Use(favicon.New("./favicon.ico"))
// 处理请求
server.GET("/json", func(ctx *gin.Context) {
// 返回json数据
ctx.JSON(200, gin.H{"msg": "Hello"})
})
// 启动服务
server.Run(":8000")
}
```
## 处理POST请求
```go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 创建一个服务
server := gin.Default()
// 处理请求
server.GET("/json", func(ctx *gin.Context) {
// 返回json数据
ctx.JSON(200, gin.H{"msg": "Hello"})
})
// post请求
server.POST("/post", func(ctx *gin.Context) {
// 返回json数据
ctx.JSON(200, gin.H{"msg": "Hello"})
})
// 启动服务
server.Run(":8000")
}
```
## 返回html页面
项目结构
```bash
$ tree
.
├── favicon.ico
├── go.mod
├── go.sum
├── main.go
├── static
│ ├── css
│ │ └── index.css
│ └── js
│ └── main.js
└── templates
└── index.html
```
templates/index.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- 加载静态资源 -->
<link rel="stylesheet" href="/static/css/index.css" />
<script src="/static/js/main.js"></script>
</head>
<body>
<h1>Hello {{.name}}</h1>
</body>
</html>
```
static/css/index.css
```css
body {
background-color: green;
}
```
static/js/main.js
```js
console.log("hello");
```
```go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
)
func main() {
// 创建一个服务
server := gin.Default()
// set favicon middleware
server.Use(favicon.New("./favicon.ico"))
// 加载模板文件
server.LoadHTMLGlob("templates/*")
// 静态资源
server.Static("/static", "./static")
// 处理请求
server.GET("/", func(ctx *gin.Context) {
// 返回json数据
ctx.HTML(
http.StatusOK,
"index.html",
gin.H{
"name": "Golang",
})
})
// 启动服务
server.Run(":8000")
}
```
### 接收参数
路由参数
```go
// 接收路由参数
// http://127.0.0.1:8000/user/100
server.GET("/user/:userId", func(ctx *gin.Context) {
userId := ctx.Param("userId")
ctx.JSON(200, gin.H{
"userId": userId,
})
})
```
查询参数
```go
// 接收查询参数
// http://127.0.0.1:8000/get?name=Tom&age=20
server.GET("/get", func(ctx *gin.Context) {
name := ctx.Query("name")
age := ctx.Query("age")
ctx.JSON(200, gin.H{
"name": name,
"age": age,
})
})
```
表单参数
```go
// POST http://127.0.0.1:8000/post-form
// "age"="20"&"name"="Tom"
server.POST("/post-form", func(ctx *gin.Context) {
name := ctx.PostForm("name")
age := ctx.PostForm("age")
// 返回json数据
ctx.JSON(http.StatusOK, gin.H{
"name": name,
"age": age,
})
})
```
json参数
```go
// POST http://127.0.0.1:8000/json
// {
// "name": "Tom",
// "age": 20
// }
server.POST("/json", func(ctx *gin.Context) {
// 解析json数据
rawData, _ := ctx.GetRawData()
var data gin.H
json.Unmarshal(rawData, &data)
// 返回json数据
ctx.JSON(http.StatusOK, data)
})
```
### 路由
```go
// 重定向 301
server.GET("/redirect", func(ctx *gin.Context) {
ctx.Redirect(http.StatusMovedPermanently, "https://www.baidu.com")
})
// not found 404
server.NoRoute(func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, "404.html", nil)
})
// 路由组
userGroup := server.Group("/user")
{
userGroup.POST("/add", func(ctx *gin.Context) {
})
userGroup.GET("/delete", func(ctx *gin.Context) {
})
}
```
### 中间件
```go
func userMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Set("userId", "100")
}
}
// 全局使用中间件
server.Use(userMiddleware())
// 单个路由使用中间件
server.GET("/user", userMiddleware(), func(ctx *gin.Context) {
userId := ctx.GetString("userId")
ctx.JSON(http.StatusOK, gin.H{
"userId": userId,
})
})
```
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
学习资料 学习资料
- 视频适合零基础(1.9.2):[【尚硅谷】Golang入门到实战教程](https://www.bilibili.com/video/BV1ME411Y71o) - 视频适合零基础(1.9.2):[【尚硅谷】Golang 入门到实战教程](https://www.bilibili.com/video/BV1ME411Y71o)
- 视频适合有基础(1.19):[golang入门到项目实战](https://www.bilibili.com/video/BV1zR4y1t7Wj) - 视频适合有基础(1.19):[golang 入门到项目实战](https://www.bilibili.com/video/BV1zR4y1t7Wj)
## 目录 ## 目录
...@@ -20,9 +20,9 @@ ...@@ -20,9 +20,9 @@
### 第二章 基本语法 ### 第二章 基本语法
- [2.1、Golang标识符、关键字、命名规则](/blog/golang/golang-identifier.md) - [2.1、Golang 标识符、关键字、命名规则](/blog/golang/golang-identifier.md)
- [2.2、Golang变量](/blog/golang/golang-variable.md) - [2.2、Golang 变量](/blog/golang/golang-variable.md)
- [2.3、Golang 常量](/blog/golang/golang-constant.md) - [2.3、Golang 常量](/blog/golang/golang-constant.md)
...@@ -48,9 +48,9 @@ ...@@ -48,9 +48,9 @@
- [3.6、Golang 结构体 struct](/blog/golang/golang-struct.md) - [3.6、Golang 结构体 struct](/blog/golang/golang-struct.md)
- [3.7、Golang接口 interface](/blog/golang/golang-interface.md) - [3.7、Golang 接口 interface](/blog/golang/golang-interface.md)
- [3.8、Golang包 package](/blog/golang/golang-package.md) - [3.8、Golang 包 package](/blog/golang/golang-package.md)
### 第四章 Golang 并发编程 ### 第四章 Golang 并发编程
...@@ -58,62 +58,60 @@ ...@@ -58,62 +58,60 @@
- [4.2、Golang 并发编程-channel](/blog/golang/golang-concurrency-channel.md) - [4.2、Golang 并发编程-channel](/blog/golang/golang-concurrency-channel.md)
- [4.3、Golang 并发编程-WaitGroup实现同步](/blog/golang/golang-concurrency-WaitGroup.md) - [4.3、Golang 并发编程-WaitGroup 实现同步](/blog/golang/golang-concurrency-WaitGroup.md)
- [4.4、Golang 并发编程-runtime包](/blog/golang/golang-concurrency-runtime.md) - [4.4、Golang 并发编程-runtime ](/blog/golang/golang-concurrency-runtime.md)
- [4.5、Golang 并发编程-Mutex 互斥锁实现同步](/blog/golang/golang-concurrency-Mutex.md) - [4.5、Golang 并发编程-Mutex 互斥锁实现同步](/blog/golang/golang-concurrency-Mutex.md)
- [4.6、Golang 并发编程-atomic原子操作](/blog/golang/golang-concurrency-atomic.md) - [4.6、Golang 并发编程-atomic 原子操作](/blog/golang/golang-concurrency-atomic.md)
- [4.7、Golang 并发编程-select switch](/blog/golang/golang-concurrency-select-switch.md) - [4.7、Golang 并发编程-select switch](/blog/golang/golang-concurrency-select-switch.md)
- [4.8、Golang 并发编程-Timer定时器](/blog/golang/golang-concurrency-Timer.md) - [4.8、Golang 并发编程-Timer 定时器](/blog/golang/golang-concurrency-Timer.md)
- [4.9、Golang 并发编程-Ticker 周期执行](/blog/golang/golang-concurrency-Ticker.md) - [4.9、Golang 并发编程-Ticker 周期执行](/blog/golang/golang-concurrency-Ticker.md)
### 第五章 Golang标准库 ### 第五章 Golang 标准库
- [5.1、Golang标准库-os](/blog/golang/golang-standard-os.md) - [5.1、Golang 标准库-os](/blog/golang/golang-standard-os.md)
- [5.2、Golang标准库-io输入输出](/blog/golang/golang-standard-io.md) - [5.2、Golang 标准库-io 输入输出](/blog/golang/golang-standard-io.md)
- [5.3、Golang标准库-ioutil](/blog/golang/golang-standard-ioutil.md) - [5.3、Golang 标准库-ioutil](/blog/golang/golang-standard-ioutil.md)
- [5.4、Golang标准库-bufio](/blog/golang/golang-standard-bufio.md) - [5.4、Golang 标准库-bufio](/blog/golang/golang-standard-bufio.md)
- [5.5、Golang标准库-log](/blog/golang/golang-standard-log.md) - [5.5、Golang 标准库-log](/blog/golang/golang-standard-log.md)
- [5.6、Golang标准库-builtin](/blog/golang/golang-standard-builtin.md) - [5.6、Golang 标准库-builtin](/blog/golang/golang-standard-builtin.md)
- [5.7、Golang标准库-bytes](/blog/golang/golang-standard-bytes.md) - [5.7、Golang 标准库-bytes](/blog/golang/golang-standard-bytes.md)
- [5.8、Golang标准库-errors](/blog/golang/golang-standard-errors.md) - [5.8、Golang 标准库-errors](/blog/golang/golang-standard-errors.md)
- [5.9、Golang标准库-sort](/blog/golang/golang-standard-sort.md) - [5.9、Golang 标准库-sort](/blog/golang/golang-standard-sort.md)
- [5.10、Golang标准库-time](/blog/golang/golang-standard-time.md) - [5.10、Golang 标准库-time](/blog/golang/golang-standard-time.md)
- [5.11、Golang标准库-json](/blog/golang/golang-standard-json.md) - [5.11、Golang 标准库-json](/blog/golang/golang-standard-json.md)
- [5.12、Golang标准库-xml](/blog/golang/golang-standard-xml.md) - [5.12、Golang 标准库-xml](/blog/golang/golang-standard-xml.md)
- [5.13、Golang标准库-math](/blog/golang/golang-standard-math.md) - [5.13、Golang 标准库-math](/blog/golang/golang-standard-math.md)
### 第六章 Golang 操作数据库
### 第六章 Golang操作数据库 - [Golang 操作 MySQL 数据库](/blog/golang/golang-mysql.md)
- [Golang操作MySQL数据库](/blog/golang/golang-mysql.md) - [Golang 操作 MongoDB 数据库](/blog/golang/golang-mongo.md)
- [Golang操作MongoDB数据库](/blog/golang/golang-mongo.md)
- [Golang ORM库 gorm](/blog/golang/golang-gorm.md)
- [Golang ORM 库 gorm](/blog/golang/golang-gorm.md)
### 第七章 Go 语言 Web 框架
- [Go 语言 Web 框架之 gin](/blog/golang/golang-gin.md)
https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da https://www.bilibili.com/video/BV1ME411Y71o?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
https://www.bilibili.com/video/BV1zR4y1t7Wj?p=119&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da https://www.bilibili.com/video/BV1zR4y1t7Wj?p=119&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
## 在线文档 ## 在线文档
[鱼皮编程学习路线](https://luxian.yupi.icu/)
Java程序员进阶之路 [https://tobebetterjavaer.com/](https://tobebetterjavaer.com/) Java程序员进阶之路 [https://tobebetterjavaer.com/](https://tobebetterjavaer.com/)
[毕向东—Java 基础知识总结(超级经典)](https://www.cnblogs.com/In-order-to-tomorrow/p/3652315.html) [毕向东—Java 基础知识总结(超级经典)](https://www.cnblogs.com/In-order-to-tomorrow/p/3652315.html)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册