关于golang:go-mod基本使用

5次阅读

共计 600 个字符,预计需要花费 2 分钟才能阅读完成。

gomod 用来对包进行治理,在 Go v1.13 默认开启。

  • 初始化

    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

  • 下载所有依赖

    go mod download
  • 移除不须要的依赖包

    go mod tidy
  • 移除指定的依赖包

    go mod edit --droprequire=packagepath
  • 我的项目迁徙合并
    倒入合并完我的项目后执行 go mod init 会生成新的 go.mod 文件。
    执行 go get ./… 下载所有依赖包

    go mod init
    go get ./...
  • 公布新版本

    go mod edit --module=yourpackagename

    能够看到此时版本曾经变到 v2.

正文完
 0