go mod基本使用

  • 初始化

    mkdir ~/goPrj/gomodTest
    go mod init gotest.com/v1

    在根目录下会生成一个go.mod文件

    添加测试代码,生成新文件gintest.go

    package main
    
    import (
        "net/http"
        "github.com/gin-gonic/gin"
    )
    
    func main() {
        ginServ := gin.Default()
        ginServ.Any("/higin", WebRoot)
        ginServ.Run(":8888")
    }
    
    func WebRoot(context *gin.Context) {
        context.String(http.StatusOK, "hello, world")
    }

    使用了gin,如果执行

    go run gintest.go

    命令,会自动下载Gin

    在 go.mod文件中会新增我们需要的Gin