23

go mod基本使用

 4 years ago
source link: https://studygolang.com/articles/30114
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

    2MvQFrn.png!web

  • 下载所有依赖

    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

    FZBNB3F.png!web

    可以看到此时版本已经变到v2.

有疑问加站长微信联系

iiUfA3j.png!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK