golang dev

配置Golang开发环境(仅介绍Mac平台)

install Homebrew

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

install golang

$ brew install go
$ go version
go version go1.11 darwin/amd64

# 配置GOROOT
$ printf "\n\nexport GOROOT=/usr/local/Cellar/go/1.11/libexec \n" >> ~/.bash_profile

# 配置GOPATH
$ mkdir -p ~/go/gopath
$ printf "export GOPATH=~/go/gopath \n" >> ~/.bash_profile

# 追加到PATH
$ printf "export GOBIN=$GOROOT/bin \n" >> ~/.bash_profile
$ printf "export PATH=$PATH:$GOBIN:$GOPATH/bin \n\n" >> ~/.bash_profile

$ source ~/.bash_profile

install privoxy

privoxy可以将http请求代理到socks5,下面将 privoxy + shadowsocks 搭配使用

$ brew install privoxy

# http代理地址
$ printf "\n\nlisten-address 0.0.0.0:7777 \n" >> /usr/local/etc/privoxy/config

# shadowsocks socks5代理地址
$ printf "forward-socks5 / 127.0.0.1:1080 .\n" >> /usr/local/etc/privoxy/config

# 内网地址不进行代理
$ printf "forward 192.168.*.*/ . \n" >> /usr/local/etc/privoxy/config
$ printf "forward 127.*.*.*/   . \n" >> /usr/local/etc/privoxy/config
$ printf "forward 10.*.*.*/    . \n" >> /usr/local/etc/privoxy/config
$ printf "forward *.xxx.com    . \n\n" >> /usr/local/etc/privoxy/config

# 启动privoxy服务
$ brew services start privoxy

# 配置http/https代理环境变量
$ printf "\n\nalias noproxy='unset http_proxy https_proxy' \n" >> ~/.bash_profile
$ printf "alias goproxy='export http_proxy=127.0.0.1:7777 https_proxy=127.0.0.1:7777' \n\n" >> ~/.bash_profile
$ source ~/.bash_profile

# 测试代理
$ goproxy
$ curl -v www.google.com
$ noproxy
$ curl -v www.google.com

install dep

$ brew install dep
$ dep version
dep:
 version     : v0.5.0
 build date  : 2018-07-26
 git hash    : 224a564
 go version  : go1.10.3
 go compiler : gc
 platform    : darwin/amd64
 features    : ImportDuringSolve=false

配置~/.netrc使dep支持私有repo,在github中生成access_token,然后添加到~/.netrc文件中即可,dep会自动使用~/.netrc中的配置,操作如下:

$ printf "\n\nmachine *.github.com
  login git_user
  password git_access_token \n" >> ~/.netrc

SEE FAQ: how-do-i-get-dep-to-authenticate-to-a-git-repo

dep可以使用branch/tag等作为版本号,一般为了保持依赖的稳定性,建议统一使用tag作为版本号。如:

$ git tag v0.0.1
$ git push --tags
# 使用tag作为版本
$ dep ensure -v -add github.com/spf13/cobra@v0.0.3

golang clone source code

$ git clone https://github.com/spf13/cobra $GOPATH/src/github.com/spf13/cobra && cd $GOPATH/src/github.com/spf13/cobra