Golang+GoLand搭建grpc開發環境


都是GFW的錯 導致我們無法在天朝局域網內 下載包下的依賴庫

不多說了 前提是你已經安裝好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鏡像 在任意路徑輸入以下命令 將下的依賴包設置成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/ 修改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/ 輸入命令:

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

gogo安裝插件

gogoprotobuf有兩個插件可以使用

  • protoc-gen-gogo:和protoc-gen-go生成的文件差不多,性能也幾乎一樣(稍微快一點點)

  • protoc-gen-gofast:生成的文件更復雜,性能也更高(快5-7倍)

//gogo
go get github.com/gogo/protobuf/protoc-gen-gogo

//gofast
go get github.com/gogo/protobuf/protoc-gen-gofast

安裝gogoprotobuf庫文件

go get github.com/gogo/protobuf/proto
go get github.com/gogo/protobuf/gogoproto  //這個不裝也沒關系
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM