go1.11.2用go mod 将eva-api挪到$GOPATH/src之外,并利用go mod管理包依赖

  1. $ cp -r $GOPATH/src/eva-api ~/works/eva-api
  2. $ go mod init eva-api 会生成文件: go.mod``$ go mod init eva-api 会生成文件: go.mod
  3. $ cat go.mod 只有一行:
module eva-api
  1. $ go build
    报错:
➜ /Users/wenke/works/eva-api git:(wenke) ✗$go build
go: finding github.com/sdming/goh latest
go: finding github.com/sdming/goh/Hbase latest
go: finding github.com/robfig/cron latest
go: finding github.com/astaxie/beego/logs latest
go: finding github.com/astaxie/beego/plugins/cors latest
go: finding github.com/astaxie/beego/context latest
go: finding github.com/astaxie/beego/orm latest
go: finding github.com/astaxie/beego/plugins latest
go: gopkg.in/[email protected]: unrecognized import path "gopkg.in/yaml.v2" (https fetch: Get https://gopkg.in/yaml.v2?go-get=1: read tcp 192.168.1.9:63567->35.196.143.184:443: read: connection reset by peer)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.org/x/net?go-get=1: dial tcp 6.6.6.6:443: i/o timeout)
go: golang.org/x/[email protected]: unrecognized import path "golang.org/x/crypto" (https fetch: Get https://golang.org/x/crypto?go-get=1: dial tcp 6.6.6.6:443: i/o timeout)
go: error loading module requirements
  1. 解决方法: $ vi go.mod 手动插入第1行下面的内容:
module eva-api

require (
	golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85
	golang.org/x/net v0.0.0-20181114220301-adae6a3d119a
)

replace (
	golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 => github.com/golang/crypto v0.0.0-20181203042331-505ab145d0a9
	golang.org/x/net v0.0.0-20181114220301-adae6a3d119a => github.com/golang/net v0.0.0-20181213202711-891ebc4b82d6
)

注意,如果执行下面的命令来修改go.mod会报错,必须手动修改如上,保证require和replace如果多行,都是在括号()里面。感觉应该是go.mod的一个解析错误,需要等golang自己发版更新!:

$ go mod edit -require=golang.org/x/[email protected]
$ go mod edit -require=golang.org/x/[email protected]
$ go mod edit -replace=golang.org/x/[email protected]=github.com/golang/crypto@latest
$ go mod edit -replace=golang.org/x/[email protected]=github.com/golang/net@latest
go: errors parsing go.mod:
/Users/wenke/works/eva-api/go.mod:5: invalid module version github.com/golang/crypto: version must be of the form v1.2.3
  1. 再次执行 $ go build 就可以了。
    同时会生成文件: go.sum

  2. 执行下面命令都正常:

  • $ go test ./tests
  • $ go run main.go

你可能感兴趣的:(Go)