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

Download GoLand: A Go IDE with extended support for JavaScript, TypeScript, and databases (jetbrains.com)
1.很重要,目录要包含两个东西pkg和src,存储一些缓存和自己项目的内容和mod出来的东西。

2.golang编辑器的下载bin
3.go的bin

目录展示

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

下载gin框架太慢就执行下面的命令,再去拉取gin框架
配置阿里云镜像dos:go env -w GOPROXY=https://goproxy.io
拉取框架gin:go get -u github.com/gin-gonic/gin 
配置settings,不想配也行,就是为了解决可能出现的爆红线问题

这样就是框架安装好了
下载完导入下载的包,编写简单的get接口
- package main
-
- import (
- "github.com/gin-gonic/gin"
- )
-
- func main() {
- // 创建一个服务
- ginServer := gin.Default()
- // 访问地址,处理我们的请求 Request Response
- ginServer.GET("/hello", func(context *gin.Context) {
- // 给前端相应json数据
- context.JSON(200, gin.H{"msg": "hello,world"})
- })
- // 服务器端口
- err := ginServer.Run(":8082")
- if err != nil {
- return
- }
- }
验证

Go支持resfulAPI post get put delete...
下边测试一个post
- package main
-
- import (
- "github.com/gin-gonic/gin"
- )
-
- func main() {
- // 创建一个服务
- ginServer := gin.Default()
- ginServer.Use()
- //连接数据库的代码
- // 访问地址,处理我们的请求 Request Response
- ginServer.GET("/hello", func(context *gin.Context) {
- // 给前端相应json数据
- context.JSON(200, gin.H{"msg": "hello,world"})
- })
- ginServer.POST("/user", func(context *gin.Context) {
- context.JSON(200, gin.H{"msg": "hello,post"})
- })
-
- // 服务器端口
- err := ginServer.Run(":8082")
- if err != nil {
- return
- }
- }

前后端传递值:前端传递JSON数据
- package main
-
- import (
- "encoding/json"
- "github.com/gin-gonic/gin"
- "net/http"
- )
-
- func main() {
- // 创建一个服务
- ginServer := gin.Default()
- ginServer.Use()
-
- //前端给后端传递json
- ginServer.POST("/json", func(context *gin.Context) {
- //request.body
- // 该方法返回的是一个切片[]byte,把切片包装成我们想要的对象
- b, _ := context.GetRawData()
- //go 语言的object可以用空接口来表示,可以接收一切事物
- var m map[string]interface{}
- // json解析 这里包装成map
- _ = json.Unmarshal(b, &m)
- // 返回前端json数据
- context.JSON(http.StatusOK, m)
-
- })
-
- // 服务器端口
- err := ginServer.Run(":8082")
- if err != nil {
- return
- }
- }

前后端传值,前端提交表单
- package main
-
- import (
- "encoding/json"
- "github.com/gin-gonic/gin"
- "net/http"
- )
-
- func main() {
- // 创建一个服务
- ginServer := gin.Default()
- ginServer.Use()
-
- //前端提交表单
- ginServer.POST("/user/add", func(context *gin.Context) {
- username := context.PostForm("username")
- password := context.PostForm("password")
- context.JSON(http.StatusOK, gin.H{
- "username": username,
- "password": password,
- })
- })
-
- // 服务器端口
- err := ginServer.Run(":8082")
- if err != nil {
- return
- }
- }
验证

路由、404、路由组
- //路由
- ginServer.GET("/test", func(context *gin.Context) {
- context.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
- })
-
- // 404
- ginServer.NoRoute(func(context *gin.Context) {
- context.HTML(http.StatusNotFound, "404.html", nil)
- })
-
- //路由组
- userGroup := ginServer.Group("/user")
- {
- userGroup.GET("add")
- }
-
- orderGroup := ginServer.Group("/order")
- {
- orderGroup.GET("/delect", func(context *gin.Context) {
- context.JSON(http.StatusOK,gin.H{
- "msg":"删除操作",
- })
- })
- }
中间件(拦截器)
- //自定义Go中间件 拦截器
- func handler() gin.HandlerFunc {
- return func(context *gin.Context) {
- // 通过自定义的中间件,设置的值,在后续处理只要调用了这个中间件的都可以拿到这里的参数
- context.Set("userSession","userid-1")
-
- // 判断
- //......
-
- context.Next() // 放过
- context.Abort() // 拦截
- }
- }
使用
找一个方法,把拦截器名字作为参数放入
- ginServer.GET("/user/info",myHandler(), func(context *gin.Context) {
- // 取出中间件中的值
- userSession := context.MustGet("userSession").(string)
- userid := context.Query("userid")
- username := context.Query("username")
- context.JSON(http.StatusOK, gin.H{
- "userid": userid,
- "username": username,
- "userSession":userSession,
- })
- })
验证

多线程
- package main
-
- import (
- "fmt"
- "time"
- )
-
- func main() {
- // go的多线程-协程
- go printgogogo()
- for i := 0; i < 1000; i++ {
- fmt.Println("main", i)
- }
- time.Sleep(time.Second * 3)
- }
- func printgogogo() {
- for i := 1; i < 1000; i++ {
- fmt.Println("printgogogogo", i)
- }
- }