赞
踩
Gin 是一个用 Go (Golang) 编写的 Web 框架。 它具有类似 martini 的 API,性能要好得多,多亏了 httprouter,速度提高了 40 倍。 如果您需要性能和良好的生产力,您一定会喜欢 Gin。
以下是Gin框架的一些主要特点和功能:
# 通过 go 直接下载gin框架 $ go get -u github.com/gin-gonic/gin go: downloading golang.org/x/net v0.22.0 go: downloading github.com/go-playground/validator/v10 v10.19.0 go: downloading github.com/pelletier/go-toml/v2 v2.2.0 go: downloading github.com/go-playground/validator v9.31.0+incompatible go: downloading github.com/pelletier/go-toml v1.9.5 go: downloading github.com/ugorji/go/codec v1.2.12 go: downloading github.com/ugorji/go v1.2.12 go: downloading google.golang.org/protobuf v1.33.0 go: downloading github.com/bytedance/sonic v1.11.3 go: downloading golang.org/x/sys v0.18.0 go: downloading github.com/gabriel-vasile/mimetype v1.4.3 go: downloading github.com/leodido/go-urn v1.4.0 go: downloading golang.org/x/crypto v0.21.0 go: downloading golang.org/x/text v0.14.0 go: downloading github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d go: downloading golang.org/x/arch v0.7.0 go: downloading github.com/klauspost/cpuid/v2 v2.2.7 go: downloading github.com/chenzhuoyu/iasm v0.9.0 go: downloading github.com/chenzhuoyu/iasm v0.9.1 go: downloading github.com/stretchr/testify v1.9.0 go: upgraded github.com/bytedance/sonic v1.9.1 => v1.11.3 go: upgraded github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 => v0.0.0-20230717121745-296ad89f973d go: added github.com/chenzhuoyu/iasm v0.9.1 go: upgraded github.com/gabriel-vasile/mimetype v1.4.2 => v1.4.3 go: upgraded github.com/go-playground/validator/v10 v10.14.0 => v10.19.0 go: upgraded github.com/klauspost/cpuid/v2 v2.2.4 => v2.2.7 go: upgraded github.com/leodido/go-urn v1.2.4 => v1.4.0 go: upgraded github.com/mattn/go-isatty v0.0.19 => v0.0.20 go: upgraded github.com/pelletier/go-toml/v2 v2.0.8 => v2.2.0 go: upgraded github.com/ugorji/go/codec v1.2.11 => v1.2.12 go: upgraded golang.org/x/arch v0.3.0 => v0.7.0 go: upgraded golang.org/x/crypto v0.9.0 => v0.21.0 go: upgraded golang.org/x/net v0.10.0 => v0.22.0 go: upgraded golang.org/x/sys v0.8.0 => v0.18.0 go: upgraded golang.org/x/text v0.9.0 => v0.14.0 go: upgraded google.golang.org/protobuf v1.30.0 => v1.33.0 # 如果下载不下来,可以考虑使用国内代理: https://goproxy.io/zh/ # 建议收藏该网址 # 里面的内容大致如下: ### Linux 或者 Mac系统使用下面 # 配置 GOPROXY 环境变量 export GOPROXY=https://goproxy.io,direct # 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选) export GOPRIVATE=git.mycompany.com,github.com/my/private ### ### Windows 的配置如下: # 配置 GOPROXY 环境变量 $env:GOPROXY = "https://goproxy.io,direct" # 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔(可选) $env:GOPRIVATE = "git.mycompany.com,github.com/my/private" ###
package main import ( "net/http" "github.com/gin-gonic/gin" ) func main() { // 创建一个默认的路由引擎 r := gin.Default() // GET:请求方式;/helloworld:请求的路径 // 当客户端以GET方法请求/helloworld路径时,会执行后面的匿名函数 r.GET("/helloworld", func(c *gin.Context) { c.String(http.StatusOK, "Hello World") }) // 启动HTTP服务,监听本机的8080端口 r.Run(":8080") }
# 运行刚刚写的程序
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery m
iddleware already attached.
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /helloworld --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080
# 这边会卡在这边
# 访问方式:
# 本地浏览器访问
http://127.0.0.1:8080/helloworld
# 浏览器中展示结果
Hello World
# console新增信息
[GIN] 2024/03/21 - 11:03:39 | 200 | 0s | 127.0.0.1 | GET "/helloworld"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。