go相关命令整理


参照:http://c.biancheng.net/view/123.html

go vendor

最开始的时候,Go 并没有提供较为妥当的包管理工具。从 1.5 版本开始提供了 vendor 特性,但需要手动设置环境变量 GO15VENDOREXPERIMENT=1。

在执行 go build 或 go run 命令时,会按照以下顺序去查找包:

当前包下的 vendor 目录
向上级目录查找,直到找到 src 下的 vendor 目录
在 GOROOT 目录下查找
在 GOPATH 下面查找依赖包
在发布 1.6 版本时,该环境变量的值已经默认设置为 1 了,该值可以使用 go env 命令查看。

在发布 1.7 版本时,已去掉该环境变量,默认开启 vendor 特性。
govendor的使用见此,摘抄几个自认为重要的

初始化

govendor init

执行此命令后,目录下生成vendor文件夹与vendor文件夹下的vendor.json
但是在当前的go1.16版本下,该命令不可被读取。应该这么执行

$ go mod vendor

生成vendor文件夹、vendor文件夹内用依赖包与modules.txt

go module

与go vendor不同的是,go module的使用让go不必一定在GOPATH下编译。

既有项目

假设你已经有了一个go 项目, 比如在$GOPATH/github.com/smallnest/rpcx下, 你可以使用go mod init github.com/smallnest/rpcx在这个文件夹下创建一个空的go.mod (只有第一行 module github.com/smallnest/rpcx)。

然后你可以通过 go get ./…让它查找依赖,并记录在go.mod文件中(你还可以指定 -tags,这样可以把tags的依赖都查找到)。

通过go mod tidy也可以用来为go.mod增加丢失的依赖,删除不需要的依赖,但是我不确定它怎么处理tags。

执行上面的命令会把go.mod的latest版本换成实际的最新的版本,并且会生成一个go.sum记录每个依赖库的版本和哈希值。

新建项目

可以在GOPATH之外建立一个文件夹
我的步骤:
step1

go mod init

生成go.mod
step2 创建 server.go

package main

import (
        "net/http"

        "github.com/labstack/echo"
)

func main() {
        e := echo.New()
        e.GET("/", func(c echo.Context) error {
                return c.String(http.StatusOK, "Hello, World!")
        })
        e.Logger.Fatal(e.Start(":1323"))
}

本来按照别人博客的写法go run server.go会自动查找依赖下载,但是我执行这个命令后给出如下提示

server.go:6:2: no required module provides package github.com/labstack/echo; to add it:
        go get github.com/labstack/echo

然后又试了一下go build,提示如下

server.go:6:2: no required module provides package github.com/labstack/echo; to add it:
        go get github.com/labstack/echo

step 3
所以我就

go get github.com/labstack/echo

step 4
再执行

go build

成功出现了可执行文件hello 还有go.sum。可以看到,依赖包下载后并没有出现在当前文件夹下
下载依赖包版本相关的问题暂时不在此处讨论

对go vendor的兼容

不翻译了
Go modules ignores the vendor/ directory by default. The idea is to eventually do away with vendoring. But if we still want to add vendored dependencies to our version control, we can still do it:

$ go mod vendor

This will create a vendor/ directory under the root of your project containing the source code for all of your dependencies.

Still, go build will ignore the contents of this directory by default. If you want to build dependencies from the vendor/ directory, you’ll need to ask for it.

$ go build -mod vendor

I expect many developers willing to use vendoring will run go build normally on their development machines and use -mod vendor in their CI.

Again, Go modules is moving away from the idea of vendoring and towards using a Go module proxy for those who don’t want to depend on the upstream version control services directly.

There are ways to guarantee that go will not reach the network at all (e.g. GOPROXY=off) but these are the subject for a future blog post.

go get

这个命令在内部实际上分成了两步操作:第一步是下载源码包,第二步是执行 go install
默认情况下,go get 可以直接使用。例如,想获取 go 的源码并编译,使用下面的命令行即可:

$ go get github.com/davyxu/cellnet

获取前,请确保 GOPATH 已经设置。Go 1.8 版本之后,GOPATH 默认在用户目录的 go 文件夹下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值