Golang+GoLand搭建grpc开发环境

2022年3月30日 170点热度 0人点赞 0条评论

图片



前景准备:

  • 已经安装好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  //这个不装也没关系
转自:https://blog.51cto.com/u_15077551/4319531

图片

图片

6240Golang+GoLand搭建grpc开发环境

root

这个人很懒,什么都没留下

文章评论