go-module
go module hello-world
############ I 配置环境变量
cat ~/.zshrc
export GOPATH=/Users/as4k/xdev/xgopath
export GOPROXY=https://goproxy.cn
export GO111MODULE=on
go env GOPATH GOPROXY GO111MODULE
############ II 创建项目目录
mkdir repo && cd repo
git init -q
#模拟自己的项目已经在git上了
git remote add origin https://github.com/my/repo
go mod init github.com/my/repo
➜ repo git:(master) ✗ pwd
/Users/as4k/xdev/repo #不要在 $GOPATH 下,创建这个目录
➜ repo git:(master) ✗ cat go.mod
module github.com/my/repo
go 1.15
############ III 书写代码
cat <<EOF > hello.go
package main
import (
"fmt"
"rsc.io/quote"
)
func main() {
fmt.Println(quote.Hello())
}
EOF
############ IV 编译、执行
go build -o hello #此时会自动下载依赖文件
./hello
➜ repo git:(master) ✗ go build -o hello
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
➜ repo git:(master) ✗ ls
go.mod go.sum hello hello.go
➜ repo git:(master) ✗ cat go.mod
module github.com/my/repo
go 1.15
require rsc.io/quote v1.5.2
➜ repo git:(master) ✗ 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=
➜ repo git:(master) ✗ ./hello
Hello, world.
➜ xgopath pwd
/Users/as4k/xdev/xgopath
➜ xgopath tree -L 3
.
├── bin
│ └── repo
└── pkg
├── mod
│ ├── cache
│ ├── golang.org
│ └── rsc.io
└── sumdb
└── sum.golang.org
8 directories, 1 file
查看 go install 安装目录
go list -f '{{.Target}}'
➜ hello ls
go.mod hello hello.go
➜ hello go list -f '{{.Target}}'
/Users/as4k/xdev/xgopath/bin/hello
参考资料
go module 基本使用
https://www.cnblogs.com/chnmig/p/11806609.html
github.com/Shopify/sarama
https://pkg.go.dev/github.com/Shopify/sarama#readme-getting-started
19. 一文搞懂 Go Modules 前世今生及入门使用
https://www.cnblogs.com/wongbingming/p/12941021.html
go module 基本使用
https://www.cnblogs.com/chnmig/p/11806609.html
https://github.com/golang/go/wiki/Modules
https://blog.golang.org/using-go-modules
http://www.topgoer.com/%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C/go%E6%93%8D%E4%BD%9Ckafka/%E6%93%8D%E4%BD%9Ckafka.html