安裝goland環境
下載golang安裝包,國內環境打開https://studygolang.com/dl
,國外環境打開https://golang.google.cn/dl/
下載對應系統的安裝包,這里以linux環境為例。
wget https://dl.google.com/go/go1.12.8.linux-amd64.tar.gz
執行安裝
// 解壓
tar xvf go1.12.8.linux-amd64.tar.gz
// 移動目錄到系統目錄
mv go /usr/local
配置環境變量,寫入GOROOT、GOPATH等必要信息
vi /etc/profile
// 寫入GOPATH、GOROOT信息
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/go/
export PATH=$PATH:$GOPATH/bin/
// 添加完成后刷新環境變量
source /etc/profile
輸入goenv查看當前golang的環境是否配置正確。
安裝Protocol Buffers v3
先到github下載穩定版安裝包wget https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protobuf-all-3.9.1.tar.gz
// 解壓
tar xvf protobuf-all-3.9.1.tar.gz
// 安裝gcc c++
參考:https://www.cnblogs.com/walkman-sky/p/9426775.html
// 執行安裝
./configure
make && make install
檢查是否安裝成功protoc --version
安裝grpc
安裝grpc有兩種方法,最簡單的是使用go get -u google.golang.org/grpc
,但是此方法需要合理上網。
第二種方法使用github安裝
cd $GOPATH/src
mkdir google.golang.org
cd google.golang.org/
git clone https://github.com/grpc/grpc-go grpc
安裝Protoc Plugin
安裝Protoc Plugin使用go get -u github.com/golang/protobuf/protoc-gen-go
安裝grpc-gateway
下載grpc-gateway主文件
安裝grpc-gateway同樣有兩種方法,go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
,直接使用go get 安裝,此方法有一些依賴需要從google下載,所以需要合理上網。國內推薦使用第二種方法:
cd $GOPATH/src/github.com
mkdir grpc-ecosystem
cd grpc-ecosystem
git clone https://github.com/grpc-ecosystem/grpc-gateway.git
編譯安裝yaml
yaml是編譯安裝protoc-gen-grpc-gateway的必備文件
cd $GOPATH/src/github.com
mkdir ghodss
cd ghodss
git clone https://github.com/ghodss/yaml.git
編譯安裝glog
cd $GOPATH/src/github.com/golang
git clone https://github.com/golang/glog.git
安裝yaml.v2
go get gopkg.in/yaml.v2
編譯安裝protoc-gen-grpc-gateway
cd $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go build
mv protoc-gen-grpc-gateway $GOPATH/bin
編譯安裝protoc-gen-swagger
cd $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go build
mv protoc-gen-swagger $GOPATH/bin