“Golang Gin Web 框架快速入门”的版本间差异
本页内容
(创建页面,内容为“== Golang 版本要求 == Go 1.13 及以上版本 == 安装 == 要安装 Gin 软件包,需要先安装 Go 并设置 Go Workspace。 1.下载并安装 gin: <sample title="" desc=""> $ go get -u github.com/gin-gonic/gin </sample> 2.将 gin 引入到代码中: <sample title="" desc=""> import "github.com/gin-gonic/gin" </sample> 3.(可选)如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包: <sample title="" desc=""> i…”) |
无编辑摘要 |
||
(未显示同一用户的4个中间版本) | |||
第1行: | 第1行: | ||
[[分类:Golang Gin Web 框架教程]] | |||
== Golang 版本要求 == | == Golang 版本要求 == | ||
Go 1.13 及以上版本 | Go 1.13 及以上版本 | ||
== 安装 == | == 安装 == | ||
要安装 Gin 软件包,需要先安装 Go 并设置 Go Workspace。 | 要安装 Gin 软件包,需要先安装 Go 并设置 Go Workspace。 | ||
<sample title="1.下载并安装 gin: | |||
1.下载并安装 gin: | " desc=""> | ||
$ go get -u github.com/gin-gonic/gin | $ go get -u github.com/gin-gonic/gin | ||
</sample> | </sample> | ||
<sample title="" desc=""> | <sample title="2.将 gin 引入到代码中: | ||
" desc=""> | |||
import "github.com/gin-gonic/gin" | import "github.com/gin-gonic/gin" | ||
</sample> | </sample> | ||
<sample title="" desc=""> | <sample title="3.(可选)如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包: | ||
" desc=""> | |||
import "net/http" | import "net/http" | ||
</sample> | </sample> | ||
<sample title="" desc=""> | <sample title="创建你的项目文件夹并 cd 进去 | ||
" desc=""> | |||
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_" | $ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_" | ||
</sample> | </sample> | ||
<sample title="" desc=""> | <sample title="拷贝一个初始模板到你的项目里 | ||
" desc=""> | |||
$ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go | $ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go | ||
</sample> | </sample> | ||
<sample title="" desc="" > | <sample title="" desc="https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go 的内容如下"> | ||
package main | |||
import ( | |||
"net/http" | |||
"github.com/gin-gonic/gin" | |||
) | |||
var db = make(map[string]string) | |||
func setupRouter() *gin.Engine { | |||
// Disable Console Color | |||
// gin.DisableConsoleColor() | |||
r := gin.Default() | |||
// Ping test | |||
r.GET("/ping", func(c *gin.Context) { | |||
c.String(http.StatusOK, "pong") | |||
}) | |||
// Get user value | |||
r.GET("/user/:name", func(c *gin.Context) { | |||
user := c.Params.ByName("name") | |||
value, ok := db[user] | |||
if ok { | |||
c.JSON(http.StatusOK, gin.H{"user": user, "value": value}) | |||
} else { | |||
c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"}) | |||
} | |||
}) | |||
// Authorized group (uses gin.BasicAuth() middleware) | |||
// Same than: | |||
// authorized := r.Group("/") | |||
// authorized.Use(gin.BasicAuth(gin.Credentials{ | |||
// "foo": "bar", | |||
// "manu": "123", | |||
//})) | |||
authorized := r.Group("/", gin.BasicAuth(gin.Accounts{ | |||
"foo": "bar", // user:foo password:bar | |||
"manu": "123", // user:manu password:123 | |||
})) | |||
/* example curl for /admin with basicauth header | |||
Zm9vOmJhcg== is base64("foo:bar") | |||
curl -X POST \ | |||
http://localhost:8080/admin \ | |||
-H 'authorization: Basic Zm9vOmJhcg==' \ | |||
-H 'content-type: application/json' \ | |||
-d '{"value":"bar"}' | |||
*/ | |||
authorized.POST("admin", func(c *gin.Context) { | |||
user := c.MustGet(gin.AuthUserKey).(string) | |||
// Parse JSON | |||
var json struct { | |||
Value string `json:"value" binding:"required"` | |||
} | |||
if c.Bind(&json) == nil { | |||
db[user] = json.Value | |||
c.JSON(http.StatusOK, gin.H{"status": "ok"}) | |||
} | |||
}) | |||
return r | |||
} | |||
func main() { | |||
r := setupRouter() | |||
// Listen and Server in 0.0.0.0:8080 | |||
r.Run(":8080") | |||
} | |||
</sample> | |||
<sample title="运行你的项目 | |||
" desc="" > | |||
$ go run main.go | $ go run main.go | ||
</sample> | </sample> | ||
开始 | == 开始 == | ||
不确定如何编写和执行 Go 代码? 可以查看[https://go.dev/doc/code 《How to Write Go Code》]. | 不确定如何编写和执行 Go 代码? 可以查看[https://go.dev/doc/code 《How to Write Go Code》]. | ||
首先,创建一个名为 example.go 的文件 | 首先,创建一个名为 example.go 的文件 | ||
<sample title="" desc="" > | <sample title="" desc="" > | ||
$ touch example.go | $ touch example.go | ||
</sample> | </sample> | ||
接下来, 将如下的代码写入 example.go 中: | 接下来, 将如下的代码写入 example.go 中: | ||
<sample title="" desc=""> | <sample title="" desc=""> | ||
package main | package main | ||
第67行: | 第136行: | ||
} | } | ||
</sample> | </sample> | ||
然后, 执行 go run example.go 命令来运行代码: | 然后, 执行 go run example.go 命令来运行代码: | ||
<sample title="" desc=""> | <sample title="" desc=""> | ||
#运行 example.go 并且在浏览器中访问 127.0.0.1:8080/ping | |||
$ go run example.go | $ go run example.go | ||
</sample> | </sample> |
2022年9月27日 (二) 14:18的最新版本
Golang 版本要求
Go 1.13 及以上版本
安装
要安装 Gin 软件包,需要先安装 Go 并设置 Go Workspace。
1.下载并安装 gin:
$ go get -u github.com/gin-gonic/gin
2.将 gin 引入到代码中:
import "github.com/gin-gonic/gin"
3.(可选)如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包:
import "net/http"
创建你的项目文件夹并 cd 进去
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
拷贝一个初始模板到你的项目里
$ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go
示例
https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go 的内容如下
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
var db = make(map[string]string)
func setupRouter() *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
r := gin.Default()
// Ping test
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
// Get user value
r.GET("/user/:name", func(c *gin.Context) {
user := c.Params.ByName("name")
value, ok := db[user]
if ok {
c.JSON(http.StatusOK, gin.H{"user": user, "value": value})
} else {
c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})
}
})
// Authorized group (uses gin.BasicAuth() middleware)
// Same than:
// authorized := r.Group("/")
// authorized.Use(gin.BasicAuth(gin.Credentials{
// "foo": "bar",
// "manu": "123",
//}))
authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
"foo": "bar", // user:foo password:bar
"manu": "123", // user:manu password:123
}))
/* example curl for /admin with basicauth header
Zm9vOmJhcg== is base64("foo:bar")
curl -X POST \
http://localhost:8080/admin \
-H 'authorization: Basic Zm9vOmJhcg==' \
-H 'content-type: application/json' \
-d '{"value":"bar"}'
*/
authorized.POST("admin", func(c *gin.Context) {
user := c.MustGet(gin.AuthUserKey).(string)
// Parse JSON
var json struct {
Value string `json:"value" binding:"required"`
}
if c.Bind(&json) == nil {
db[user] = json.Value
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
})
return r
}
func main() {
r := setupRouter()
// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
}
运行你的项目
$ go run main.go
开始
不确定如何编写和执行 Go 代码? 可以查看《How to Write Go Code》.
首先,创建一个名为 example.go 的文件
示例
$ touch example.go
接下来, 将如下的代码写入 example.go 中:
示例
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}
然后, 执行 go run example.go 命令来运行代码:
示例
#运行 example.go 并且在浏览器中访问 127.0.0.1:8080/ping
$ go run example.go