使用go mod管理golang模块

开始

  1. go版本需要>=1.11,将项目移到GOPATH以外,

或者显式开启go mod 模式export GO111MODULE=on

  1. 首次使用在项目根目录执行

go mod github.xsky.com/user_name/your_module

该命令会生成go.mod 文件,里面是将来项目的具体依赖,依赖一般会在go build/go test等命令执行时候自动拉取。

比如我一个项目build 完成后式这样的


module github.xsky.com/xsky/xsky-csi

require (

github.com/container-storage-interface/spec v1.0.0

github.com/docker/distribution v2.7.1+incompatible // indirect

github.com/gogo/protobuf v1.2.0 // indirect

github.com/golang/groupcache v0.0.0-20181024230925-c65c006176ff // indirect

github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect

github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect

github.com/google/uuid v1.1.0 // indirect

github.com/googleapis/gnostic v0.2.0 // indirect

github.com/gregjones/httpcache v0.0.0-20181110185634-c63ab54fda8f // indirect

github.com/hashicorp/golang-lru v0.5.0 // indirect

github.com/json-iterator/go v1.1.5 // indirect

github.com/kubernetes-csi/drivers v0.0.0-20181129192734-8a7f2d3a4057

github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

github.com/modern-go/reflect2 v1.0.1 // indirect

github.com/onsi/ginkgo v1.7.0 // indirect

github.com/onsi/gomega v1.4.3 // indirect

github.com/opencontainers/go-digest v1.0.0-rc1 // indirect

github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709

github.com/peterbourgon/diskv v2.0.1+incompatible // indirect

github.com/prometheus/client_golang v0.9.2 // indirect

github.com/sirupsen/logrus v1.3.0

github.com/spf13/afero v1.2.0 // indirect

github.com/spf13/pflag v1.0.3 // indirect

github.com/stretchr/testify v1.3.0 // indirect

golang.org/x/net v0.0.0-20181201002055-351d144fa1fc

golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be

golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35 // indirect

golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect

google.golang.org/genproto v0.0.0-20181202183823-bd91e49a0898 // indirect

google.golang.org/grpc v1.16.0

gopkg.in/inf.v0 v0.9.1 // indirect

gopkg.in/yaml.v2 v2.2.2 // indirect

k8s.io/api v0.0.0-20181221193117-173ce66c1e39 // indirect

k8s.io/apiextensions-apiserver v0.0.0-20190115195204-f00e89d6e347 // indirect

k8s.io/apimachinery v0.0.0-20190111195121-fa6ddc151d63 // indirect

k8s.io/apiserver v0.0.0-20190115192846-d7664e065129 // indirect

k8s.io/client-go v10.0.0+incompatible // indirect

k8s.io/cloud-provider v0.0.0-20181221204816-2325825fd8d8 // indirect

k8s.io/csi-api v0.0.0-20190119024756-9225cacc8063 // indirect

//k8s.io/apimachinery v0.0.0-20181127025237-2b1284ed4c93

k8s.io/klog v0.1.0 // indirect

k8s.io/kube-openapi v0.0.0-20190115222348-ced9eb3070a5 // indirect

k8s.io/kubernetes v1.14.0-alpha.1

//k8s.io/kubernetes v1.13.0

k8s.io/utils v0.0.0-20190123133337-dfffd27c1d21

sigs.k8s.io/yaml v1.1.0 // indirect

//k8s.io/utils v0.0.0-20181115163542-0d26856f57b3

)

3.这些依赖是指定了版本,存放在

$GOPATH/pkg/mod/*

比如~/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]

和vendor不一样,这些是包可以复用的,只要go.mod里面的版本对应得上即可。

然后这里build的时候遇到了一个问题

# k8s.io/apiextensions-apiserver/pkg/features

../../workspace/gopath/pkg/mod/k8s.io/[email protected]/pkg/features/kube_features.go:52:2: undefined: feature.DefaultMutableFeatureGate

通过切换不同版本也是类似的错误

# k8s.io/kubernetes/pkg/features

../../workspace/gopath/pkg/mod/k8s.io/[email protected]/pkg/features/kube_features.go:399:32: feature.DefaultFeatureGate.Add undefined (type feature.FeatureGate has no field or method Add)

从错误上看是这样的:

获取到的依赖[email protected]内容

s

实际GitHub上的源码:

src

就是说实际依赖是错误的,源码都已经更新了

到golang仓库下提了issue,

得到回复

The problem here is some sort of version skew between the various kubernetes modules. (See https://github.com/kubernetes/apiextensions-apiserver#compatibility.)

解决办法:

显式下载最新的go module

go get -v -m k8s.io/[email protected] k8s.io/[email protected] k8s.io/[email protected] k8s.io/[email protected]

然后把go.mod里的这几个依赖版本写上刚才获取的这个几个,或者直接删除掉,然后执行下面整理不必要的包

go mod tidy

然后全部build一次

go build ./... or go build all 此时应该已经构建通过了

另外几个很有用的命令

go clean -modcache 清理下载的go 模块缓存

go list -m -json all 查看build的时候最终使用的模块详情


❯ go list -m -json all

{

"Path": "github.xsky.com/xsky/xsky-csi",

"Main": true,

"Dir": "/Users/xsky/Documents/tmp/xsky-csi-mod",

"GoMod": "/Users/xsky/Documents/tmp/xsky-csi-mod/go.mod"

}


{

"Path": "github.com/onsi/ginkgo",

"Version": "v1.7.0",

"Time": "2018-11-21T17:19:17Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/onsi/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/onsi/ginkgo/@v/v1.7.0.mod"

}

{

"Path": "github.com/onsi/gomega",

"Version": "v1.4.3",

"Time": "2018-11-21T17:14:07Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/onsi/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/onsi/gomega/@v/v1.4.3.mod"

}

{

"Path": "github.com/opencontainers/go-digest",

"Version": "v1.0.0-rc1",

"Time": "2017-06-07T19:53:33Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/opencontainers/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/opencontainers/go-digest/@v/v1.0.0-rc1.mod"

}

{

"Path": "github.com/pborman/uuid",

"Version": "v0.0.0-20180906182336-adf5a7427709",

"Time": "2018-09-06T18:23:36Z",

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/pborman/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/pborman/uuid/@v/v0.0.0-20180906182336-adf5a7427709.mod"

}

{

"Path": "github.com/peterbourgon/diskv",

"Version": "v2.0.1+incompatible",

"Time": "2017-08-14T17:35:58Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/peterbourgon/[email protected]+incompatible",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/peterbourgon/diskv/@v/v2.0.1+incompatible.mod"

}

{

"Path": "github.com/pmezard/go-difflib",

"Version": "v1.0.0",

"Time": "2016-01-10T10:55:54Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/pmezard/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/pmezard/go-difflib/@v/v1.0.0.mod"

}

{

"Path": "github.com/sirupsen/logrus",

"Version": "v1.3.0",

"Time": "2019-01-02T22:40:11Z",

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/sirupsen/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/sirupsen/logrus/@v/v1.3.0.mod"

}

{

"Path": "github.com/spf13/afero",

"Version": "v1.2.0",

"Time": "2018-12-17T08:55:40Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/spf13/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/spf13/afero/@v/v1.2.0.mod"

}



{

"Path": "k8s.io/api",

"Version": "v0.0.0-20181221193117-173ce66c1e39",

"Time": "2018-12-21T19:31:17Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/k8s.io/api/@v/v0.0.0-20181221193117-173ce66c1e39.mod"

}

{

"Path": "k8s.io/apiextensions-apiserver",

"Version": "v0.0.0-20190115195204-f00e89d6e347",

"Time": "2019-01-15T19:52:04Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/k8s.io/apiextensions-apiserver/@v/v0.0.0-20190115195204-f00e89d6e347.mod"

}

{

"Path": "k8s.io/apimachinery",

"Version": "v0.0.0-20190111195121-fa6ddc151d63",

"Time": "2019-01-11T19:51:21Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/k8s.io/apimachinery/@v/v0.0.0-20190111195121-fa6ddc151d63.mod"

}

{

"Path": "k8s.io/apiserver",

"Version": "v0.0.0-20190115192846-d7664e065129",

"Time": "2019-01-15T19:28:46Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/k8s.io/apiserver/@v/v0.0.0-20190115192846-d7664e065129.mod"

}

{

"Path": "sigs.k8s.io/yaml",

"Version": "v1.1.0",

"Time": "2018-11-02T19:02:23Z",

"Indirect": true,

"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/sigs.k8s.io/[email protected]",

"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/sigs.k8s.io/yaml/@v/v1.1.0.mod"

}