Skip to content

后端部分

后端使用go语言加MySQL数据库,go语言的框架:gingorm用于数据库的连接

环境搭建

  1. 安装go的环境:https://go.dev/dl/

  2. 安装后将\Go\bin 目录添加到 Path 环境变量中

  3. 下载GoLand,打开新建的go-crud文件夹

  4. 新建main.go文件,点击终端,在终端中输入go mod init go-crud新建go.mod文件

  5. 下载gingormmysql

  6. 下载Gin包:go get -u github.com/gin-gonic/gin

    若出现:go: module github.com/gin-gonic/gin: Get "https://proxy.golang.org/github.com/gin-gonic/gin/@v/list": dial tcp 142.251.43.17:443: i/o timeout,则执行如下两行后在安装Gin包:

    • go env -w GO111MODULE=on

    • go env -w GOPROXY=https://goproxy.cn,direct

    在代码中导入:import "github.com/gin-gonic/gin"

  7. 下载gorm包:

    • go get -u gorm.io/gorm
    • go get -u gorm.io/driver/sqlite
  8. 在代码中导入:

    go
    import (
        "gorm.io/gorm"
        "gorm.io/driver/sqlite"
    )

    导入mysql包:import "gorm.io/driver/mysql"

  9. 通过navicat创建一个mysql数据库,字符集选择utf8mb4,表明可以在数据库中存放一些表情

Released under the MIT License.