安装指南

安装

获取可安装的版本:

  1. brew search go | egrep '^go$|^go@.*$'

安装最新版本:

  1. brew install go

安装指定版本:

  1. brew install go@1.14
  2. brew install go@1.16

获取已安装的版本:

  1. brew list | grep go
  2. # Inter
  3. ll -d /usr/local/Cellar/go*
  4. # M1
  5. ll -d /opt/homebrew/Cellar/go*

获取当前使用的版本:

  1. go version

切换版本:

  1. brew unlink go
  2. brew link go@1.14

创建工作目录:

  1. mkdir -p /Users/${USER}/Desktop/Project/golang

环境变量

设置环境变量:

  1. go env -w GOOS="darwin"
  2. go env -w GOARCH="amd64"
  3. go env -w GOPATH="/Users/${USER}/Desktop/Project/golang"
  4. go env -w GOBIN="$(go env GOPATH)/bin"
  5. go env -w GO111MODULE="auto"
  6. go env -w GOPROXY="https://goproxy.io,https://proxy.golang.org,https://goproxy.cn,direct"
  7. echo "" >> ~/.zshrc
  8. echo "export GOPATH='/Users/${USER}/Desktop/Project/golang'" >> ~/.zshrc
  9. echo 'export PATH="${PATH}:${GOPATH}/bin"' >> ~/.zshrc
  1. go env -w GOOS="darwin"
  2. go env -w GOARCH="arm64"
  3. go env -w GOPATH="/Users/${USER}/Desktop/Project/golang"
  4. go env -w GOBIN="$(go env GOPATH)/bin"
  5. go env -w GO111MODULE="auto"
  6. go env -w GOPROXY="https://goproxy.io,https://proxy.golang.org,https://goproxy.cn,direct"
  7. echo "" >> ~/.zshrc
  8. echo "export GOPATH='/Users/${USER}/Desktop/Project/golang'" >> ~/.zshrc
  9. echo 'export PATH="${PATH}:${GOPATH}/bin"' >> ~/.zshrc
  1. go env -w GOOS="linux"
  2. go env -w GOARCH="amd64"
  3. go env -w GOPATH="/opt/golang"
  4. go env -w GOBIN="$(go env GOPATH)/bin"
  5. go env -w GO111MODULE="auto"
  6. go env -w GOPROXY="https://goproxy.io,https://proxy.golang.org,https://goproxy.cn,direct"
  7. echo "" >> ~/.bashrc
  8. echo "export GOPATH='/opt/golang'" >> ~/.bashrc
  9. echo 'export PATH="${PATH}:${GOPATH}/bin"' >> ~/.bashrc
  1. echo '' >> ~/.ssh/config
  2. echo 'Host e.coding.net' >> ~/.ssh/config
  3. echo ' Port 22' >> ~/.ssh/config
  4. echo '' >> ~/.gitconfig
  5. echo '[url "https://pt60y7hineu6:c3bccede2849bd974993b68ff55e23e13e2ca80e@e.coding.net/bitdesk/nodehub"]' >> ~/.gitconfig
  6. echo ' insteadOf = https://e.coding.net/bitdesk/nodehub' >> ~/.gitconfig
  7. go env -w GONOPROXY="e.coding.net"
  8. go env -w GONOSUMDB="e.coding.net"
  9. go env -w GOPRIVATE="e.coding.net"

获取环境变量:

  1. # 获取所有变量
  2. go env
  3. # 获取指定变量
  4. go env GOOS

还原环境变量:

  1. go env -u GOOS

模块管理

初始化项目:

  1. # 可执行
  2. go mod init
  3. # 库
  4. go mod init github.com/go-agumon/table

根据go.mod文件处理依赖关系(查缺补漏)

  1. go mod tidy

将依赖包复制到项目下的vendor目录

  1. go mod vendor

获取依赖关系

  1. go list -m all