golang 最被诟病的三个问题: module 管理机制,泛型,错误处理
go mod就是用来解决 module 管理机制.
首先,把go的版本升级到1.12.x. 可以用go help mod看到go mod的子命令.
Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.
Usage:
go mod [arguments]
The commands are:
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
Use "go help mod " for more information about a command.
其次,在项目目录下,一般是main.go(func main()所在的源文件)所在的目录下,执行go mod init 项目名(我这里是go-wikitten).这个命令会在执行命令的当前目录下,生成一个go.mod 文件,内容如下:
module go-wikitten
go 1.12
最重要的一步来了,因为众所周知的原因,很多包不能直接下载,可以用proxy,如果找不到proxy,那么只能用replace.用文本编辑器打开go.mod,加入如下内容:
module go-wikitten
go 1.12
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
)
然后执行 go mod tidy.这个命令会把latest自动替换成最新的版本号.例子:
module go-wikitten
go 1.12
replace (
golang.org/x/sync => github.com/golang/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys => github.com/golang/sys v0.0.0-20190613124609-5ed2794edfdc
golang.org/x/text => github.com/golang/text v0.3.2
golang.org/x/tools => github.com/golang/tools v0.0.0-20190613204242-ed0dc450797f
golang.ort/x/net => github.com/golang/net v0.0.0-20190613194153-d28f0bde5980
)
如果是老项目,一般不会那么容易就成功,会提示各种错误,比如出现如下错误
go: golang.org/x/net@v0.0.0-20190404232315-eb5bcb51f2a3: git fetch -f https://go.googlesource.com/net refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in D:\workspace\okr\pkg\mod\cache\vcs\4a22365141bc4eea5d5ac4a1395e653f2669485db75ef119e7bbec8e19b12a21: exit status 128:
fatal: unable to access 'https://go.googlesource.com/net/': Failed to connect to go.googlesource.com port 443: Timed out
go: error loading module requirements
其实就是提示net包除了最新版之外,还需要其它的版本v0.0.0-20190404232315-eb5bcb51f2a3(出错信息@后面的部分),所以我们手工修改go.mod,加入一行:
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 => github.com/golang/net v0.0.0-20190404232315-eb5bcb51f2a3
然后再执行go mod tidy.如果出错,再把需要的版本加入,反复几次后,最后应该就会成功了.我最后的go.mod是这样的:
module go-wikitten
go 1.12
replace (
golang.org/x/crypto => github.com/golang/crypto v0.0.0-20190611184440-5c40567a22f8
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 => github.com/golang/net v0.0.0-20181220203305-927f97764cc3
golang.org/x/net v0.0.0-20190311183353-d8887717615a => github.com/golang/net v0.0.0-20190311183353-d8887717615a
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 => github.com/golang/net v0.0.0-20190404232315-eb5bcb51f2a3
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c => github.com/golang/net v0.0.0-20190503192946-f4e77d36d62c
golang.org/x/sync => github.com/golang/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys => github.com/golang/sys v0.0.0-20190613124609-5ed2794edfdc
golang.org/x/text => github.com/golang/text v0.3.2
golang.org/x/tools => github.com/golang/tools v0.0.0-20190613204242-ed0dc450797f
golang.ort/x/net => github.com/golang/net v0.0.0-20190613194153-d28f0bde5980
gopkg.in/russross/blackfriday.v2 => github.com/russross/blackfriday v2.0.0+incompatible
gopkg.in/yaml.v2 => github.com/go-yaml/yaml v2.1.0+incompatible
)
require (
github.com/davidrjenni/reftools v0.0.0-20190411195930-981bbac422f8
github.com/dchest/captcha v0.0.0-20170622155422-6a29415a8364
github.com/fatih/motion v0.0.0-20190527122956-41470362fad4
github.com/fsnotify/fsnotify v1.4.7
github.com/gin-gonic/gin v1.4.0
github.com/gorilla/context v1.1.1
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.1.3
github.com/keegancsmith/rpc v1.1.0
github.com/kisielk/errcheck v1.2.0
github.com/kisielk/gotool v1.0.0
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.1.1
github.com/lxmgo/config v0.0.0-20180313024057-8db99aca0f7e
github.com/mattn/go-isatty v0.0.8
github.com/mdempsky/gocode v0.0.0-20190203001940-7fb65232883f
github.com/microcosm-cc/bluemonday v1.0.2
github.com/mozillazg/go-pinyin v0.15.0
github.com/pkg/errors v0.8.1
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518
github.com/stamblerre/gocode v0.0.0-20190327203809-810592086997
github.com/stretchr/testify v1.3.0
go4.org v0.0.0-20190313082347-94abd6928b1d
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c
golang.org/x/sys v0.0.0-20190412213103-97732733099d
golang.org/x/text v0.3.0
golang.org/x/tools v0.0.0-20190408220357-e5b8258f4918
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1
gopkg.in/go-playground/validator.v8 v8.18.2
gopkg.in/russross/blackfriday.v2 v2.0.0-00010101000000-000000000000
)
最后,附上常用的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
cloud.google.com/go => github.com/googleapis/google-cloud-go v0.34.0
github.com/go-tomb/tomb => gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7
go.opencensus.io => github.com/census-instrumentation/opencensus-go v0.19.0
go.uber.org/atomic => github.com/uber-go/atomic v1.3.2
go.uber.org/multierr => github.com/uber-go/multierr v1.1.0
go.uber.org/zap => github.com/uber-go/zap v1.9.1
google.golang.org/api => github.com/googleapis/google-api-go-client v0.0.0-20181220000619-583d854617af
google.golang.org/appengine => github.com/golang/appengine v1.3.0
google.golang.org/genproto => github.com/google/go-genproto v0.0.0-20181219182458-5a97ab628bfb
google.golang.org/grpc => github.com/grpc/grpc-go v1.17.0
gopkg.in/alecthomas/kingpin.v2 => github.com/alecthomas/kingpin v2.2.6+incompatible
gopkg.in/mgo.v2 => github.com/go-mgo/mgo v0.0.0-20180705113604-9856a29383ce
gopkg.in/vmihailenco/msgpack.v2 => github.com/vmihailenco/msgpack v2.9.1+incompatible
gopkg.in/yaml.v2 => github.com/go-yaml/yaml v0.0.0-20181115110504-51d6538a90f8
labix.org/v2/mgo => github.com/go-mgo/mgo v0.0.0-20160801194620-b6121c6199b7
launchpad.net/gocheck => github.com/go-check/check v0.0.0-20180628173108-788fd7840127