go 1.11 引入了 mod 预览特性,用来管理依赖。go 1.12 正式支持。
示例1
$ mkdir sql2dsl
$ cd sql2dsl
$ go mod init sql2dsl
$ ls
go.mod
上面的命令中,go mod init sql2dsl
用于初始化模块。
go.mod 文件内容如下:
module sql2dsl
go 1.12
在 sql2dsl
目录中编写文件 main.go :
package main
import (
"fmt"
"github.com/cch123/elasticsql"
"github.com/tidwall/pretty"
)
var sql = `
select * from user_info
where id=1
`
func main() {
dsl, esType, _ := elasticsql.Convert(sql)
// 打印 type 名称
fmt.Println(esType)
fmt.Println()
// 打印 dsl
fmt.Println(dsl)
fmt.Println()
// 打印格式化后的 dsl
var result = pretty.Pretty([]byte(dsl))
fmt.Println(string(result))
}
运行:
$ go run main.go
go: finding github.com/tidwall/pretty v1.0.0
go: finding github.com/cch123/elasticsql v1.0.0
go: downloading github.com/tidwall/pretty v1.0.0
go: extracting github.com/tidwall/pretty v1.0.0
go: downloading github.com/cch123/elasticsql v1.0.0
go: extracting github.com/cch123/elasticsql v1.0.0
go: finding github.com/xwb1989/sqlparser latest
go: downloading github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
go: extracting github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
user_info
{"query" : {"bool" : {"must" : [{"match" : {"id" : {"query" : "1", "type" : "phrase"}}}]}},"from" : 0,"size" : 1}
{
"query": {
"bool": {
"must": [
{
"match": {
"id": {
"query": "1",
"type": "phrase"
}
}
}
]
}
},
"from": 0,
"size": 1
}
第一次运行时,前面部分是下载依赖,后面是执行结果。
我们再执行一次 main.go ,不会再次下载依赖:
$ go run main.go
user_info
{"query" : {"bool" : {"must" : [{"match" : {"id" : {"query" : "1", "type" : "phrase"}}}]}},"from" : 0,"size" : 1}
{
"query": {
"bool": {
"must": [
{
"match": {
"id": {
"query": "1",
"type": "phrase"
}
}
}
]
}
},
"from": 0,
"size": 1
}
go.sum 文件
在这个示例中,执行 main.go 之后,sql2dsl
目录下 出现了一个 go.sum
文件,内容如下:
github.com/cch123/elasticsql v1.0.0 h1:7SbQw3ZAImsajjY7lx2798n816J+rer8HrfDt4k+e8w=
github.com/cch123/elasticsql v1.0.0/go.mod h1:h4Tt1A91nOVAYsWdoxlXwKYPfxkxeTuRFkEMUQaRVBo=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY=
记录的是版本号和哈希值。
依赖下载到哪里去了 ?
$GOPATH/pkg/mod/
目录。
将依赖挪到本项目的 vendor 目录
$ go mod vendor
示例2
项目结构如下:
$ tree myapp
myapp
├── go.mod
├── main.go
└── utils
└── common.go
go.mod 内容如下:
module myapp
go 1.12
utils/common.go 内容如下:
package utils
func Add(a int, b int) int {
return a+b
}
main.go 内容如下:
package main
import "fmt"
import utils "myapp/utils"
func main() {
fmt.Println(utils.Add(1,2))
}
运行 main.go :
$ go run main.go
3
go mod 子命令
$ go mod help
Go mod provides access to operations on modules.
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 <command> [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 <command>" for more information about a command.