简介
go.modgo.modgo.modGOPATHGOPATHgo.modGOPATH
创建模块
$GOPATH/srchello.go
package hello
func Hello() string {
return "Hello, world"
}
hello_test.go
package hello
import "testing"
func TestHello(t *testing.T) {
want := "Hello,world"
if got := Hello(); got != want {
t.Errorf("Hello() = %q, want %q", got, want)
}
}
go.modgo test
D:\Code\go>go test
PASS
ok _/D_/Code/go 0.327s
D:\Code\go>
$GOPATH_/D_/Code/gogo mod initgo test
D:\Code\go>go mod init example.com/hello
go: creating new go.mod: module example.com/hello
D:\Code\go>go test
PASS
ok example.com/hello 0.393s
D:\Code\go>
go mod initgo.mod
PS D:\Code\go> cat .\go.mod module example.com/hello
go 1.12
PS D:\Code\go>
go.modworldgo mod initexample.com/helloexample.com/hello/world
添加依赖
hello.gorsc.io/quote
package hello
import "rsc.io/quote"
func Hello() string {
return quote.Hello()
}
go test
D:\Code\go>go test
go: finding golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: downloading rsc.io/sampler v1.3.0
go: extracting rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: extracting golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
--- FAIL: TestHello (0.00s)
hello_test.go:10: Hello() = "Hello, world.", want "Hello, world"
FAIL
exit status 1
FAIL example.com/hello 0.431s
D:\Code\go>
go.modgo.modgo.modgo testrsc.io/quotersc.io/quotersc.io/samplergolang.org/x/testgo.mod
D:\Code\go>type go.mod
module example.com/hello
go 1.12
require rsc.io/quote v1.5.2
D:\Code\go>
go testgo.modgo list -m
yankeweideMacBook-Pro:hello yankewei$ go list -m all
hello
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
rsc.io/quote v1.5.2
rsc.io/sampler v1.3.0
yankeweideMacBook-Pro:hello yankewei$
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
yankeweideMacBook-Pro:hello yankewei$ cat go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
yankeweideMacBook-Pro:hello yankewei$
go使用go.sum文件来确保未来下载这些模块的时候保证和第一次下载的一样,也确保你项目的依赖不会改变,所以go.mod和go.sum都应该纳入版本控制中。
更新依赖
go list -m allgolang.org/x/text
$ go get golang.org/x/text
go: finding golang.org/x/text v0.3.0
go: downloading golang.org/x/text v0.3.0
go: extracting golang.org/x/text v0.3.0
$ go test
PASS
ok example.com/hello 0.013s
$
go list -m all
$ go list -m all
example.com/hello
golang.org/x/text v0.3.0
rsc.io/quote v1.5.2
rsc.io/sampler v1.3.0
$ cat go.mod
module example.com/hello
go 1.12
require (
golang.org/x/text v0.3.0 // indirect
rsc.io/quote v1.5.2
)
$
golang.org/x/testindirectgo help modules
rsc.io/sampler
$ go get rsc.io/sampler
go: finding rsc.io/sampler v1.99.99
go: downloading rsc.io/sampler v1.99.99
go: extracting rsc.io/sampler v1.99.99
$ go test
--- FAIL: TestHello (0.00s)
hello_test.go:8: Hello() = "99 bottles of beer on the wall, 99 bottles of beer, ...", want "Hello, world."
FAIL
exit status 1
FAIL example.com/hello 0.014s
$
Shit!测试失败了,最新的版本和我们要使用的不兼容,先看一下这个模块的可用版本:
$ go list -m -versions rsc.io/sampler
rsc.io/sampler v1.0.0 v1.2.0 v1.2.1 v1.3.0 v1.3.1 v1.99.99
$
我们已经使用过v1.3.0;v1.99.99版本不兼容,可以尝试使用v1.3.1:
$ go get rsc.io/sampler@v1.3.1
go: finding rsc.io/sampler v1.3.1
go: downloading rsc.io/sampler v1.3.1
go: extracting rsc.io/sampler v1.3.1
$ go test
PASS
ok example.com/hello 0.022s
$
go get@v1.3.1@latest
添加一个新的主版本
func Proverbrsc.io/quote/v3quote.Concurrency
package hello
import (
"rsc.io/quote"
quoteV3 "rsc.io/quote/v3"
)
func Hello() string {
return quote.Hello()
}
func Proverb() string {
return quoteV3.Concurrency()
}
然后在hello_test.go添加一个测试:
func TestProverb(t *testing.T) {
want := "Concurrency is not parallelism."
if got := Proverb(); got != want {
t.Errorf("Proverb() = %q, want %q", got, want)
}
}
现在可以执行代码:
$ go test
go: finding rsc.io/quote/v3 v3.1.0
go: downloading rsc.io/quote/v3 v3.1.0
go: extracting rsc.io/quote/v3 v3.1.0
PASS
ok example.com/hello 0.024s
$
rsc.io/quotersc.io/quote/v3
更新依赖为新的主版本
rsc.io/quotersc.io/quote/v3
$ go doc rsc.io/quote/v3
package quote // import "rsc.io/quote/v3"
Package quote collects pithy sayings.
func Concurrency() string
func GlassV3() string
func GoV3() string
func HelloV3() string
func OptV3() string**
可以看到原来的 Hello 改为了 HelloV3,那我们就可以把使用的 Hello 改为 HelloV3,并且文件中没有对旧版本的依赖了,可以把导入重命名去掉:
package hello
import (
"rsc.io/quote/v3"
)
func Hello() string {
return quote.HelloV3()
}
func Proverb() string {
return quote.Concurrency()
}
可以自行测试,就不演示了。
删除未使用的依赖
go mod tidy