npmcomposerMavenGradle
Depofficial experiment
go1.11module
VendorGoVendorGoDepDepGlide

关于Workspaces和GOPATH

go1.11go get
Workspaces
GOPATH
GOPATH
$ echo $HOME
/Users/wangtom
$ export GOPATH=$HOME/goworkspace
GOPATH
$ go env |grep GOPATH                    
GOPATH="/Users/wangtom/goworkspace"

开启module特性

GO111MODULE

开启 GO111MODULE

GO111MODULE=on

创建模块

mytest
github.com/cnwyt/mytest
$GOPATH/src/github.com

创建一个目录mytest目录,并进入该命令:

$ mkdir -p $GOPATH/src/github.com/cnwyt/mytest
$ cd $GOPATH/src/github.com/cnwyt/mytest
go mod init
$ go mod init mytest                                                                                            
go: modules disabled inside GOPATH/src by GO111MODULE=auto; 
see 'go help modules'

开启 GO111MODULE:

GO111MODULE=automodulesmodulesGO111MODULEon
$ export GO111MODULE=on  
$ go mod init github.com/cnwyt/mytest   
go: creating new go.mod: module github.com/cnwyt/mytest
GOPATH

调用go modules:

mytest
$ mkdir helloworld && cd helloworld
$ vi main.go
main.go
package main

import "fmt"
//import "github.com/cnwyt/mytest"

func main() {
    fmt.Println("Hello, World!");

    //mytest.ShowTest1()
}
github.com/cnwyt/mytestlatest
$ go mod init helloworld
$ go mod edit -require github.com/cnwyt/mytest@latest
go.modpackage.jsoncomposer.json
module helloworld

require github.com/cnwyt/mytest v0.0.0
go testgo run main.go
$ go test
build helloworld: cannot find module for path github.com/cnwyt/mytest
github.com/cnwyt/mytestGOPATHGOPATH
cnwyt/mytestcnwyt/mytestgo replace
go.mod
module helloworld

require github.com/cnwyt/mytest v0.0.0

replace github.com/cnwyt/mytest => /Users/wangtom/goworkspace/mytest
v0.0.0latest

然后重新运行即可:

$ go test                                                    
go: finding github.com/cnwyt/mytest v0.0.0
?   	helloworld	[no test files]
*_test.gogo test

调用第三方模块

gorilla/mux
go.modrequirelatest
require github.com/gorilla/mux latest
go buildgo testgo.mod
latestv1.6.2
module helloworld

require github.com/cnwyt/mytest v0.0.0
require github.com/gorilla/mux v1.6.2

replace github.com/cnwyt/mytest => /Users/wangtom/goworkspace/godict
gorilla/mux$GOPATH/pkg/mod/
$ ll /Users/wangtom/goworkspace/pkg/mod/github.com/gorilla 
total 0
dr-xr-xr-x  22 wangtom  staff 704B 12 24 22:14 mux@v1.6.2

[END]