小白教程
所有教程
关于
Search
172.69.7.125
172.69.7.125
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Golang Gin Web 框架快速入门”的源代码
本页内容
上一节:
Golang_Gin_Web_框架教程
下一节:
Golang_Gin_Web_框架HTML模版渲染
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
[[分类:Golang Gin Web 框架教程]] == Golang 版本要求 == Go 1.13 及以上版本 == 安装 == 要安装 Gin 软件包,需要先安装 Go 并设置 Go Workspace。 <sample title="1.下载并安装 gin: " desc=""> $ go get -u github.com/gin-gonic/gin </sample> <sample title="2.将 gin 引入到代码中: " desc=""> import "github.com/gin-gonic/gin" </sample> <sample title="3.(可选)如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包: " desc=""> import "net/http" </sample> <sample title="创建你的项目文件夹并 cd 进去 " desc=""> $ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_" </sample> <sample title="拷贝一个初始模板到你的项目里 " desc=""> $ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go </sample> <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 </sample> == 开始 == 不确定如何编写和执行 Go 代码? 可以查看[https://go.dev/doc/code 《How to Write Go Code》]. 首先,创建一个名为 example.go 的文件 <sample title="" desc="" > $ touch example.go </sample> 接下来, 将如下的代码写入 example.go 中: <sample title="" desc=""> 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 上启动服务 } </sample> 然后, 执行 go run example.go 命令来运行代码: <sample title="" desc=""> #运行 example.go 并且在浏览器中访问 127.0.0.1:8080/ping $ go run example.go </sample>
返回至“
Golang Gin Web 框架快速入门
”。
上一节:
Golang_Gin_Web_框架教程
下一节:
Golang_Gin_Web_框架HTML模版渲染