- go版本需要>=1.11,将项目移到GOPATH以外
或者显式开启go mod 模式export GO111MODULE=on
- 首次使用在项目根目录执行
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
)
- 这些依赖是指定了版本,存放在
$GOPATH/pkg/mod/*
比如~/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]
和vendor不一样,这些是包可以复用的,只要go.mod里面的版本对应得上即可。
然后这里build的时候遇到了一个问题
../../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]内容
实际GitHub上的源码:
就是说实际依赖是错误的,源码都已经更新了
到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/google/uuid",
"Version": "v1.1.0",
"Time": "2018-09-17T14:00:05Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/google/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/google/uuid/@v/v1.1.0.mod"
}
{
"Path": "github.com/googleapis/gnostic",
"Version": "v0.2.0",
"Time": "2018-05-19T18:57:00Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/googleapis/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/googleapis/gnostic/@v/v0.2.0.mod"
}
{
"Path": "github.com/kubernetes-csi/drivers",
"Version": "v0.0.0-20181129192734-8a7f2d3a4057",
"Time": "2018-11-29T19:27:34Z",
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/kubernetes-csi/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/kubernetes-csi/drivers/@v/v0.0.0-20181129192734-8a7f2d3a4057.mod"
}
{
"Path": "github.com/matttproud/golang_protobuf_extensions",
"Version": "v1.0.1",
"Time": "2016-04-24T11:30:07Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/matttproud/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/matttproud/golang_protobuf_extensions/@v/v1.0.1.mod"
}
{
"Path": "github.com/modern-go/concurrent",
"Version": "v0.0.0-20180306012644-bacd9c7ef1dd",
"Time": "2018-03-06T01:26:44Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/modern-go/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/modern-go/concurrent/@v/v0.0.0-20180306012644-bacd9c7ef1dd.mod"
}
{
"Path": "github.com/modern-go/reflect2",
"Version": "v1.0.1",
"Time": "2018-07-18T01:23:57Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/modern-go/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/modern-go/reflect2/@v/v1.0.1.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": "github.com/spf13/pflag",
"Version": "v1.0.3",
"Time": "2018-08-31T15:14:32Z",
"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/pflag/@v/v1.0.3.mod"
}
{
"Path": "github.com/stretchr/objx",
"Version": "v0.1.1",
"Time": "2018-01-25T22:19:50Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/stretchr/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/stretchr/objx/@v/v0.1.1.mod"
}
{
"Path": "github.com/stretchr/testify",
"Version": "v1.3.0",
"Time": "2019-01-03T19:31:39Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/github.com/stretchr/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/github.com/stretchr/testify/@v/v1.3.0.mod"
}
{
"Path": "golang.org/x/crypto",
"Version": "v0.0.0-20180904163835-0709b304e793",
"Time": "2018-09-04T16:38:35Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/golang.org/x/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/golang.org/x/crypto/@v/v0.0.0-20180904163835-0709b304e793.mod"
}
{
"Path": "golang.org/x/lint",
"Version": "v0.0.0-20180702182130-06c8688daad7",
"Time": "2018-07-02T18:21:30Z",
"Indirect": true,
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/golang.org/x/lint/@v/v0.0.0-20180702182130-06c8688daad7.mod"
}
{
"Path": "golang.org/x/net",
"Version": "v0.0.0-20181201002055-351d144fa1fc",
"Time": "2018-12-01T00:20:55Z",
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/golang.org/x/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/golang.org/x/net/@v/v0.0.0-20181201002055-351d144fa1fc.mod"
}
{
"Path": "gopkg.in/fsnotify.v1",
"Version": "v1.4.7",
"Time": "2018-01-10T05:33:47Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/gopkg.in/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/gopkg.in/fsnotify.v1/@v/v1.4.7.mod"
}
{
"Path": "gopkg.in/inf.v0",
"Version": "v0.9.1",
"Time": "2018-03-26T17:23:32Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/gopkg.in/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/gopkg.in/inf.v0/@v/v0.9.1.mod"
}
{
"Path": "gopkg.in/tomb.v1",
"Version": "v1.0.0-20141024135613-dd632973f1e7",
"Time": "2014-10-24T13:56:13Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/gopkg.in/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/gopkg.in/tomb.v1/@v/v1.0.0-20141024135613-dd632973f1e7.mod"
}
{
"Path": "gopkg.in/yaml.v2",
"Version": "v2.2.2",
"Time": "2018-11-15T11:05:04Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/gopkg.in/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/gopkg.in/yaml.v2/@v/v2.2.2.mod"
}
{
"Path": "honnef.co/go/tools",
"Version": "v0.0.0-20180728063816-88497007e858",
"Time": "2018-07-28T06:38:16Z",
"Indirect": true,
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/honnef.co/go/tools/@v/v0.0.0-20180728063816-88497007e858.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": "k8s.io/client-go",
"Version": "v10.0.0+incompatible",
"Time": "2018-12-04T00:07:44Z",
"Indirect": true,
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]+incompatible",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/k8s.io/client-go/@v/v10.0.0+incompatible.mod"
}
{
"Path": "k8s.io/cloud-provider",
"Version": "v0.0.0-20181221204816-2325825fd8d8",
"Time": "2018-12-21T20:48:16Z",
"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/cloud-provider/@v/v0.0.0-20181221204816-2325825fd8d8.mod"
}
{
"Path": "k8s.io/csi-api",
"Version": "v0.0.0-20190119024756-9225cacc8063",
"Time": "2019-01-19T02:47:56Z",
"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/csi-api/@v/v0.0.0-20190119024756-9225cacc8063.mod"
}
{
"Path": "k8s.io/klog",
"Version": "v0.1.0",
"Time": "2018-11-13T19:59:58Z",
"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/klog/@v/v0.1.0.mod"
}
{
"Path": "k8s.io/kube-openapi",
"Version": "v0.0.0-20190115222348-ced9eb3070a5",
"Time": "2019-01-15T22:23:48Z",
"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/kube-openapi/@v/v0.0.0-20190115222348-ced9eb3070a5.mod"
}
{
"Path": "k8s.io/kubernetes",
"Version": "v1.14.0-alpha.1",
"Time": "2019-01-15T10:46:36Z",
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/k8s.io/kubernetes/@v/v1.14.0-alpha.1.mod"
}
{
"Path": "k8s.io/utils",
"Version": "v0.0.0-20190123133337-dfffd27c1d21",
"Time": "2019-01-23T13:33:37Z",
"Dir": "/Users/xsky/Documents/workspace/gopath/pkg/mod/k8s.io/[email protected]",
"GoMod": "/Users/xsky/Documents/workspace/gopath/pkg/mod/cache/download/k8s.io/utils/@v/v0.0.0-20190123133337-dfffd27c1d21.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"
}