go mod是1.11以后版本新增的,如果是1.9.2及以下的版本是没有gomod的。
>go version
go version go1.12.5 windows/386
我们来新建一个项目比如 webApp,完整路径:
C:\Work\Project\VSCORE\webApp
首先是初始化模块
go mod init<项目模块名称>
#初始化模块,会在项目根目录下生成 go.mod文件。
go mod tidy
#根据go.mod文件来处理依赖关系。
如果是从github上拉下来的项目,执行这个命令之后就会开始下载一些需要的mod,比如下面的demo
go: finding github.com/tidwall/gjson v1.2.1
go: finding github.com/gomodule/redigo/redis latest
go: finding github.com/go-sql-driver/mysql v1.4.1go: finding github.com/astaxie/beego/cache/redis latest
go: finding github.com/astaxie/beego/orm latest
go: finding github.com/astaxie/beego/cache latest
go: finding github.com/astaxie/beego v1.11.1
go: finding github.com/astaxie/beego/logs latest
go: finding github.com/astaxie/beego/context/param latest
go: downloading github.com/tidwall/gjson v1.2.1
go: extracting github.com/tidwall/gjson v1.2.1
go: finding github.com/yinheli/mahonia latest
go: finding github.com/gomodule/redigo v2.0.0+incompatible
go: downloading github.com/gomodule/redigo v2.0.0+incompatible
go: extracting github.com/gomodule/redigo v2.0.0+incompatible
go: finding github.com/gomodule/redigo/redisx latest
go: finding github.com/tidwall/match v1.0.1
go: downloading github.com/tidwall/match v1.0.1
go: extracting github.com/tidwall/match v1.0.1
go: downloading github.com/go-sql-driver/mysql v1.4.1
go: extracting github.com/go-sql-driver/mysql v1.4.1
go: finding github.com/pkg/errors v0.8.1
go: downloading github.com/pkg/errors v0.8.1
go: extracting github.com/pkg/errors v0.8.1
go: downloading github.com/astaxie/beego v1.11.1
go: downloading github.com/yinheli/mahonia v0.0.0-20131226213531-0eef680515cc
go: extracting github.com/yinheli/mahonia v0.0.0-20131226213531-0eef680515cc
go: finding github.com/astaxie/beego/context latest
go: extracting github.com/astaxie/beego v1.11.1
go: finding github.com/pkg/errors v0.8.0
go: finding github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c
go: finding github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58
go: finding github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542
go: finding github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d
go: finding github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737
go: finding github.com/mattn/go-sqlite3 v1.10.0
go: finding github.com/pelletier/go-toml v1.2.0
go: finding github.com/elazarl/go-bindata-assetfs v1.0.0
go: finding gopkg.in/yaml.v2 v2.2.1
go: finding github.com/Knetic/govaluate v3.0.0+incompatible
go: finding github.com/go-redis/redis v6.14.2+incompatible
go: finding github.com/cupcake/rdb v0.0.0-20161107195141-43ba34106c76
go: finding github.com/gogo/protobuf v1.1.1
go: finding github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd
go: finding github.com/casbin/casbin v1.7.0
go: finding github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db
go: finding github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a
go: finding github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712
go: finding github.com/lib/pq v1.0.0
go: finding github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec
go: finding github.com/couchbase/gomemcached v0.0.0-20181122193126-5125a94a666c
go: finding github.com/couchbase/go-couchbase v0.0.0-20181122212707-3e9b6e1258bb
go: finding github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726
go: finding gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: finding github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b
go: golang.org/x/net@v0.0.0-20181114220301-adae6a3d119a: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
go: finding github.com/belogik/goes v0.0.0-20151229125003-e54d722c3aff
go: finding github.com/siddontang/ledisdb v0.0.0-20181029004158-becf5f38d373
go: golang.org/x/crypto@v0.0.0-20181127143415-eb0de9b17e85: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
go: error loading module requirements
呃,报错了,好吧 golang.org 这个网站打不开,可以通过替换github地址来下载mod
replace (
golang.org/x/text => github.com/golang/text latest
golang.org/x/net => github.com/golang/net latest
golang.org/x/crypto => github.com/golang/crypto latest
golang.org/x/tools => github.com/golang/tools latest
golang.org/x/sync => github.com/golang/sync latest
golang.org/x/sys => github.com/golang/sys latest
cloud.google.com/go => github.com/googleapis/google-cloud-go latest
google.golang.org/genproto => github.com/google/go-genproto latest
golang.org/x/exp => github.com/golang/exp latest
golang.org/x/time => github.com/golang/time latest
golang.org/x/oauth2 => github.com/golang/oauth2 latest
golang.org/x/lint => github.com/golang/lint latest
google.golang.org/grpc => github.com/grpc/grpc-go latest
google.golang.org/api => github.com/googleapis/google-api-go-client latest
google.golang.org/appengine => github.com/golang/appengine latest
golang.org/x/mobile => github.com/golang/mobile latest
golang.org/x/image => github.com/golang/image latest
)
放到go.mod文件里就行,当然也可以使用命令
go mod edit -replace=golang.org/x/crypto@v0.0.0=github.com/golang/crypto@latest
然后再执行一次 tidy
go mod tidy -v
有疑问加站长微信联系(非本文作者)