• GoLang开发使用gin框架搭建web程序


    目录

    1.SDK安装

    ​2.编辑器下载

    3.编辑器准备

    4.使用

    4.1常见请求方式


    1.SDK安装

    保证装了Golang的sdk(官网下载windows.zip->解压,安装,配置bin的环境变量)

    2.编辑器下载

    Download GoLand: A Go IDE with extended support for JavaScript, TypeScript, and databases (jetbrains.com)

    3.环境配置

    1.很重要,目录要包含两个东西pkg和src,存储一些缓存和自己项目的内容和mod出来的东西。

    2.golang编辑器的下载bin

    3.go的bin

    目录展示

    4.编辑器准备

    打开新建的goweb项目,进入编辑器最下方的终端,执行go mod init

    下载gin框架太慢就执行下面的命令,再去拉取gin框架

    配置阿里云镜像dos:go env -w GOPROXY=https://goproxy.io
    拉取框架gin:go get -u github.com/gin-gonic/gin  

    配置settings,不想配也行,就是为了解决可能出现的爆红线问题

    这样就是框架安装好了

    5.使用

    下载完导入下载的包,编写简单的get接口

    1. package main
    2. import (
    3. "github.com/gin-gonic/gin"
    4. )
    5. func main() {
    6. // 创建一个服务
    7. ginServer := gin.Default()
    8. // 访问地址,处理我们的请求 Request Response
    9. ginServer.GET("/hello", func(context *gin.Context) {
    10. // 给前端相应json数据
    11. context.JSON(200, gin.H{"msg": "hello,world"})
    12. })
    13. // 服务器端口
    14. err := ginServer.Run(":8082")
    15. if err != nil {
    16. return
    17. }
    18. }

    验证

    5.1常见请求方式

    Go支持resfulAPI  post get put delete...
    下边测试一个post

    1. package main
    2. import (
    3. "github.com/gin-gonic/gin"
    4. )
    5. func main() {
    6. // 创建一个服务
    7. ginServer := gin.Default()
    8. ginServer.Use()
    9. //连接数据库的代码
    10. // 访问地址,处理我们的请求 Request Response
    11. ginServer.GET("/hello", func(context *gin.Context) {
    12. // 给前端相应json数据
    13. context.JSON(200, gin.H{"msg": "hello,world"})
    14. })
    15. ginServer.POST("/user", func(context *gin.Context) {
    16. context.JSON(200, gin.H{"msg": "hello,post"})
    17. })
    18. // 服务器端口
    19. err := ginServer.Run(":8082")
    20. if err != nil {
    21. return
    22. }
    23. }

    前后端传递值:前端传递JSON数据

    1. package main
    2. import (
    3. "encoding/json"
    4. "github.com/gin-gonic/gin"
    5. "net/http"
    6. )
    7. func main() {
    8. // 创建一个服务
    9. ginServer := gin.Default()
    10. ginServer.Use()
    11. //前端给后端传递json
    12. ginServer.POST("/json", func(context *gin.Context) {
    13. //request.body
    14. // 该方法返回的是一个切片[]byte,把切片包装成我们想要的对象
    15. b, _ := context.GetRawData()
    16. //go 语言的object可以用空接口来表示,可以接收一切事物
    17. var m map[string]interface{}
    18. // json解析 这里包装成map
    19. _ = json.Unmarshal(b, &m)
    20. // 返回前端json数据
    21. context.JSON(http.StatusOK, m)
    22. })
    23. // 服务器端口
    24. err := ginServer.Run(":8082")
    25. if err != nil {
    26. return
    27. }
    28. }


    前后端传值,前端提交表单

    1. package main
    2. import (
    3. "encoding/json"
    4. "github.com/gin-gonic/gin"
    5. "net/http"
    6. )
    7. func main() {
    8. // 创建一个服务
    9. ginServer := gin.Default()
    10. ginServer.Use()
    11. //前端提交表单
    12. ginServer.POST("/user/add", func(context *gin.Context) {
    13. username := context.PostForm("username")
    14. password := context.PostForm("password")
    15. context.JSON(http.StatusOK, gin.H{
    16. "username": username,
    17. "password": password,
    18. })
    19. })
    20. // 服务器端口
    21. err := ginServer.Run(":8082")
    22. if err != nil {
    23. return
    24. }
    25. }

    验证

    路由、404、路由组

    1. //路由
    2. ginServer.GET("/test", func(context *gin.Context) {
    3. context.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
    4. })
    5. // 404
    6. ginServer.NoRoute(func(context *gin.Context) {
    7. context.HTML(http.StatusNotFound, "404.html", nil)
    8. })
    9. //路由组
    10. userGroup := ginServer.Group("/user")
    11. {
    12. userGroup.GET("add")
    13. }
    14. orderGroup := ginServer.Group("/order")
    15. {
    16. orderGroup.GET("/delect", func(context *gin.Context) {
    17. context.JSON(http.StatusOK,gin.H{
    18. "msg":"删除操作",
    19. })
    20. })
    21. }

    中间件(拦截器)

    1. //自定义Go中间件 拦截器
    2. func handler() gin.HandlerFunc {
    3. return func(context *gin.Context) {
    4. // 通过自定义的中间件,设置的值,在后续处理只要调用了这个中间件的都可以拿到这里的参数
    5. context.Set("userSession","userid-1")
    6. // 判断
    7. //......
    8. context.Next() // 放过
    9. context.Abort() // 拦截
    10. }
    11. }

    使用
    找一个方法,把拦截器名字作为参数放入

    1. ginServer.GET("/user/info",myHandler(), func(context *gin.Context) {
    2. // 取出中间件中的值
    3. userSession := context.MustGet("userSession").(string)
    4. userid := context.Query("userid")
    5. username := context.Query("username")
    6. context.JSON(http.StatusOK, gin.H{
    7. "userid": userid,
    8. "username": username,
    9. "userSession":userSession,
    10. })
    11. })

    验证

    多线程

    1. package main
    2. import (
    3. "fmt"
    4. "time"
    5. )
    6. func main() {
    7. // go的多线程-协程
    8. go printgogogo()
    9. for i := 0; i < 1000; i++ {
    10. fmt.Println("main", i)
    11. }
    12. time.Sleep(time.Second * 3)
    13. }
    14. func printgogogo() {
    15. for i := 1; i < 1000; i++ {
    16. fmt.Println("printgogogogo", i)
    17. }
    18. }

  • 相关阅读:
    搞AI开发,你不得不会的PyCharm技术
    日常思考-多个用户做出同样的请求,该请求在代码中调用同一个方法,那么请问服务器是怎么处理的?
    js对象的声明及使用
    【CGSize Objecive-C语言】
    虚拟机搭载Linux · VMware + Ubuntu 部署 路线参考(20.04.5)
    qt响应全局热键
    OpenCV学习(五)——图像基本操作(访问图像像素值、图像属性、感兴趣区域ROI和图像边框)
    【Javascrpt】比较,逻辑运算符
    SpringBoot如何集成Log模块呢?
    (PMIC)全、半桥驱动器CSD95481RWJ PDF 规格
  • 原文地址:https://blog.csdn.net/m0_55627541/article/details/133792435