简介
1.1 介绍
- Gin 是一个golang的微框架,封装比较优雅,API友好,源码注释比较明确,容错方便等特点
- 对于Golang 而言,web框架的依赖要远比Python,Java之类来的小。自身的net/http 足够简单,性能也非常不错
- 借助框架开发,不仅可以省去很多常用的封装带来的时间,也有助于团队的编码风格和形成规范
1.2 安装
因为 goland.com 需要跳墙,所以需要设置阿里镜像或者七牛镜像
七牛镜像
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
阿里云镜像
go env -w GO111MODULE=on
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
接着在执行
go get -u github.com/gin-gonic/gin
Goland 配置环境变量
Windows + R ,cmd打开控制台 go env
初步使用实例
package main
import "github.com/gin-gonic/gin"
import "net/http"
func main() {
// 创建服务
ginServer := gin.Default()
// 访问地址
ginServer.GET("/hello", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{"message": "hello world"})
})
ginServer.GET("/getHhh", func(context *gin.Context) {
context.JSON((http.StatusOK, "hhh")
})
ginServer.Run(":8080")
}