environment

install golang

First you should download the golang pkg, please choose the version as per your platform. Here is an example for Mac.

set environment variable

If you don't set $GOPATH, the default $GOPATH will be ~/go. When we run go file, firstly is bult then run. In buld process, go will search for the imported package from $GOPATH then try to install, if no package found, it will throw error.

cd 
mkdir go
# of cource you can edit .zshrc, same effect
vim .bash_profile
export GO111MODULE=on
export GOPROXY=https://goproxy.io
export GOPATH="/Users/apple/go"
export PATH=$GOPATH/bin:$PATH
source .bash_profile

intsall gopm

In theory, we can download everything with go get, but if you are in China, you should install gopm instead since GOOGLE is blocked.

# install gopm
go get -v github.com/gpmgo/gopm
# install goimports: goimports is a useful plugin. When you press Ctrl+S, it will import the package automatically.
# If the package is not used, will remove automatically as well.
gopm get -g -v -u golang.org/x/tools/cmd/goimports
cd $GOPATH
go install golang.org/x/tools/cmd/goimports

In reality, gopm can't do eveything, like this package github.com/tealeg/xlsx, when you run :

gopm get -g -v -u github.com/tealeg/xlsx

It will throw error: timeout, so you'd better prepare a VPN, like SSR.

Here I will assue you've got a VPN or SSR, next we will make some configuration, vim .zshrc

# where proxy
proxy () {
  export http_proxy="http://0.0.0.0:1087"
  export https_proxy="http://0.0.0.0:1087"
  echo "HTTP Proxy on"
}

# where noproxy
noproxy () {
  unset http_proxy
  unset https_proxy
  echo "HTTP Proxy off"
}

With proxy and noproxy command, you can easily control the proxy.

some suggestion

  • If you want to develop your personal pkg, aviod using go get, as far as I know, it has some bug. If your pkg has some error, it won't update the pkg, and throw error each time you run the program. Using gopm is a good way

results matching ""

    No results matching ""