Golang+GoLand搭建grpc開發環境
都是GFW的錯 導致我們無法在天朝區域網內 下載http://google.golang.org和http://golang.org包下的依賴庫
不多說了 前提是你已經安裝好Golang 配置好GOROOT GOBIN GOPATH環境變數 並且已經安裝好GoLand IDE
步驟1 安裝glide 使用命令 這兩個命令是用來安裝依賴管理工具glide
go get github.com/Masterminds/glide
go install github.com/Masterminds/glide
步驟2 進入項目路徑 使用命令glide init 生成glide.yaml 這個文件類似maven的pom.xml
glide init
步驟3 設置glide鏡像 在任意路徑輸入以下命令 將http://golang.org和http://google.golang.org下的依賴包設置成github的路徑作為鏡像
glide mirror set https://golang.org/x/mobile https://github.com/golang/mobile --vcs git
glide mirror set https://golang.org/x/crypto https://github.com/golang/crypto --vcs git
glide mirror set https://golang.org/x/net https://github.com/golang/net --vcs git
glide mirror set https://golang.org/x/tools https://github.com/golang/tools --vcs git
glide mirror set https://golang.org/x/text https://github.com/golang/text --vcs git
glide mirror set https://golang.org/x/image https://github.com/golang/image --vcs git
glide mirror set https://golang.org/x/sys https://github.com/golang/sys --vcs git
glide mirror set https://google.golang.org/grpc https://github.com/grpc/grpc-go --vcs git
步驟4 進入$GOPATH/src/http://github.com/Masterminds/glide/path 修改winbug.go文件如下:
func CustomRename(o, n string) error {
if runtime.GOOS == "windows" {
msg.Debug("Detected Windows. Moving files using windows command")
//cmd := exec.Command("cmd.exe", "/c", "move", o, n)
cmd := exec.Command("cmd.exe", "/c", "xcopy /s/y", o, n+"")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Error moving files: %s. output: %s", err, output)
}
return nil
} else if detectWsl() {
cmd := exec.Command("mv", o, n)
output, err2 := cmd.CombinedOutput()
msg.Debug("Detected Windows Subsystem for Linux. Removing files using subsystem command")
if err2 != nil {
return fmt.Errorf("Error moving files: %s. output: %s", err2, output)
}
return nil
}
return os.Rename(o, n)
}
就修改這一個方法 注釋掉的那一行替換為下面那一行
步驟5 進入$GOPATH/src/http://github.com/src/github.com/Masterminds/glide 輸入命令:
go build glide.go
重新編譯這個工具 在同一目錄內生成的glide.exe文件替換$GOBIN目錄下的同名文件
步驟6 進入項目路徑 運行
glide get --all-dependencies -s -v google.golang.org/grpc
glide install
通過glide拉取grpc以及相關依賴庫
步驟7 從google/protobuf下載protoc 這是生成指定語言相關類的工具 我下載的是windows版本的就是一個zip文件 zip解壓後的exe文件存在環境變數路徑中
步驟8 獲取protoc-gen-go 這是將proto文件生成go類的插件 並編譯
go get github.com/golang/protobuf/protoc-gen-go
cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go
go build
編譯完成後 會在同一個目錄內生成protoc-gen-go.exe文件 將這個文件放入環境變數路徑中
步驟9 獲取protobuf中go相關支持庫 並編譯安裝成為全局依賴
go get github.com/golang/protobuf/proto
cd $GOPATH/github.com/golang/protobuf/proto
go build
go install
步驟10 編寫proto文件(略過)
步驟11 用protoc編譯生成go文件 執行命令:
protoc --go_out=plugins=grpc:. com/xxx/yyy/zzz.proto
推薦閱讀: