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
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 //这个不装也没关系
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2020-10-15 定时任务管理工具cronsun部署