一,安装go环境
这个挺容易,不过要记得配置go环境变量,配置地址: ~/.bash_profile(环境以mac为参照)
这个也容易,不过网上基本上都收费,但也有破解的方法,自己找吧
三,安装包由于原go的默认包地址在墙外,基本上没有VPN的话,下载失败,但也有解决办法
解决方式:
开启代理:export GO111MODULE=on
设置代理:export GOPROXY=https://goproxy.cn
安装包:go get -u -v github.com/gin-gonic/gin(由于是基于gin框架做的开发,所以记得安装这个包)
以上是临时生效方式,永久设置为:
vim .base_profile文件
将export GO111MODULE=on
export GOPROXY=https://goproxy.cn写到文件中
执行source .base_profile使配置文件生效
ps: 配置好后,不要在goland中直接在import处点击sync dependencies of project按钮,这种方式默认访问的包域名还是在墙外;请在命令行中使用go get -u -v github.com/xxx/xxx的方式引进包
有可能会出现的问题:go modules文件夹消失,所有加载包均不存在
解决办法:进入go.mod文件,右键sync dependencies of project即可解决
如果,为了使用方便,右键sync包,需要在goland中配置代理:
四,测试API 4.1 静态文件绑定记得在当前文件夹下,新建文件夹assets, static, 及每个文件夹下新建html示例文件
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.Static("/assets", "./assets")
r.StaticFS("/static", http.Dir("static"))
r.StaticFile("/favicon.ico" , "favicon.ico")
r.Run(":8081")
}
运行:进入示例main.go文件所在的文件夹,执行go build -o xxxx && ./xxxx
4.2 获取/user/:id中id值package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/user/:id", func(c *gin.Context) {
c.JSON(200, gin.H{
"id": c.Param("id")
})
})
r.Run(":8081")
}
运行后测试:curl -X GET "http://127.0.0.1:8081/user/3"
4.3 泛绑定,/user/*actionpackage main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/user/*action", func(c *gin.Context) {
c.String(200, "hello world")
})
r.Run(":8081")
}
build main.go文件后,测试:curl "http:127.0.0.1:8081/user/hahhaha/3333/ssss"
4.4 url中参数,/user?name=hahah&id=123package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context) {
name := c.Query("name")
id := c.DefaultQuery("id", 77)
c.String(http.StatusOK, "%s,%s", id, name)
})
r.Run(":8081")
}
build main.go后,测试curl -X GET "http://127.0.0.1:8081/user?name=hahhah"
4.5 body中参数package main
import (
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
func main() {
r := gin.Default()
r.POST("/user", func(c *gin.Context) {
bodyByts,err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
c.Abort()
}
c.String(http.StatusOK, string(bodyByts))
})
r.Run(":8081")
}
build main.go后,测试 curl -X POST "http://127.0.0.1:8081/user" -d '{"id":888,"name":"hahhah"}'
若body中为 “first_param=其实&last_param=白色的”,
package main
import (
"bytes"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
func main() {
r := gin.Default()
r.POST("/test", func(c *gin.Context) {
bodyByts,err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
c.Abort()
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyByts)) //回存获取到的body内容
firstParam := c.PostForm("first_param")
lastParam := c.DefaultPostForm("last_param", "hahahah")
c.String(http.StatusOK, "%s, %s, %s" , firstParam, lastParam, string(bodyByts))
})
r.Run(":8081")
}
运行
4.6 将参数对照结果体进行转化package main
import (
"github.com/gin-gonic/gin"
"net/http"
"time"
)
type User struct {
Name string `form:"name"`
Address string `form:"address"`
Birthday time.Time `form:"birthday" time_format:"2006-01-02"`
}
func main() {
r := gin.Default()
r.GET("/user", testing)
r.POST("/user", testing)
r.Run(":8081")
}
func testing(c *gin.Context) {
var user User
//根据content-type来做不同binding *** 作
if err := c.ShouldBind(&user); err == nil {
c.String(http.StatusOK, "%v", user)
} else {
c.String(http.StatusBadRequest, "user bind error: %v", err)
}
}
运行:
4.7校验请求参数自定义校验规则
package main
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"time"
)
type Booking struct {
CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn" time_format:"2006-01-02"`
}
//自定义规则函数
func bookableDate(f1 validator.FieldLevel) bool {
date, ok := f1.Field().Interface().(time.Time)
if ok {
today := time.Now()
if today.After(date) {
return false
}
}
return true
}
func main() {
r := gin.Default()
//将自定义校验方法注入到validator
if v,ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("bookabledate", bookableDate)
}
r.GET("/bookable", func(c *gin.Context) {
var b Booking
if err := c.ShouldBind(&b); err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, gin.H{
"message": "ok!",
"booking": b,
})
})
r.Run(":8081")
}
运行:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)