栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

linux下golang protoc安装详细教程

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux下golang protoc安装详细教程

1. 安装 golang (1) ubuntu
add-apt-repository ppa:longsleep/golang-backports

更新 apt库
apt-get update

安装go
sudo apt-get install golang-go

鉴定是否安装成功
go version
# 设置 mod
go env -w GO111MODULE="on"
# 设置 代理
go env -w GOPROXY="https://goproxy.cn,direct"
(2)centos
yum install golang
2.下载 protoc 和 protoc-gen-go

这里直接下载编译好的版本,比较简单

工具下载地址图片
protocprotoc
protoc-gen-goprotoc-gen-go

上面的方式是图形化下载,你需要下载后再发送到你的 linux 服务器上

3. 解压并拷贝到目录
[root@centos go]# ll
total 1812
# protobuf-1.5.2.tar.gz 用来编译  protoc-gen-go
-rw-r--r-- 1 root root  171702 Jun 18 15:27 protobuf-1.5.2.tar.gz
# protoc-3.17.3-linux-x86_64.zip 是 protoc 的执行文件和 include 文件夹
-rw-r--r-- 1 root root 1680819 Jun 18 15:29 protoc-3.17.3-linux-x86_64.zip

你需要做的

  1. 将 protobuf-1.5.2.tar.gz 解压 进入 protoc-gen-go 编译 go build -o prootc-gen-go main.go 并移动到 /usr/bin
  2. 将 protoc-3.17.3-linux-x86_64.zip 解压, 给 bin/protoc 执行文件执行权限,并复制到 /usr/bin
  3. protoc-3.17.3-linux-x86_64.zip 解压的文件的 include 文件夹复制到 /usr/local/ 下面

命令如下

protoc-gen-go
[root@centos temp]# ls
protobuf-1.5.2.tar.gz  protoc-3.17.3-linux-x86_64.zip
# 解压
[root@centos temp]# tar -zxvf protobuf-1.5.2.tar.gz 
protobuf-1.5.2/
......
protobuf-1.5.2/test.bash
[root@centos temp]# cd protobuf-1.5.2/protoc-gen-go/
# 编译
[root@centos protoc-gen-go]# go build -o protoc-gen-go main.go 
[root@centos protoc-gen-go]# ls
descriptor  generator  grpc  main.go  plugin  protoc-gen-go
# 复制到 `/usr/bin` 
[root@centos protoc-gen-go]# cp protoc-gen-go /usr/bin
cp: overwrite '/usr/bin/protoc-gen-go'? y
[root@centos protoc-gen-go]# 
复制 protoc
[root@centos temp]# mkdir tmp
[root@centos temp]# ls
protobuf-1.5.2  protobuf-1.5.2.tar.gz  protoc-3.17.3-linux-x86_64.zip  tmp
# 解压
[root@centos temp]# unzip protoc-3.17.3-linux-x86_64.zip -d tmp/
Archive:  protoc-3.17.3-linux-x86_64.zip
   .....     
  inflating: tmp/readme.txt          
# 移动可执行文件 protoc
[root@centos temp]# cp tmp/bin/protoc /usr/bin
[root@centos temp]# 


复制 include 文件夹
# 移动 include 文件夹
[root@centos temp]# cp -R tmp/include /usr/local
[root@centos temp]# protoc --version
libprotoc 3.17.3
[root@centos temp]# protoc-gen-go --version
protoc-gen-go: unknown argument "--version" (this program should be run by protoc, not directly)
4. 测试

编写一个 user.proto 文件

syntax = "proto3";
package proto1;
option go_package = "proto1/";
service User {
  rpc GetUser(GetUserRequest) returns (GetUserResponse) {}
}
message GetUserRequest {
  string id = 1;
}
message GetUserResponse {
  UserDTO user = 1;
}
message UserDTO {
  string id = 1 ;
  string name = 2;
}

编译自动生成 go 文件

[root@centos temp]# ls
protobuf-1.5.2  protobuf-1.5.2.tar.gz  protoc-3.17.3-linux-x86_64.zip  tmp
[root@centos temp]# vim user.proto
[root@centos temp]# ls
protobuf-1.5.2  protobuf-1.5.2.tar.gz  protoc-3.17.3-linux-x86_64.zip  tmp  user.proto
[root@centos temp]# protoc --go_out=plugins=grpc,paths=source_relative:. ./user.proto 
[root@centos temp]# ls
protobuf-1.5.2  protobuf-1.5.2.tar.gz  protoc-3.17.3-linux-x86_64.zip  tmp  user.pb.go  user.proto
[root@centos temp]# 

参考: https://segmentfault.com/a/1190000021456180 解决编译问题
问题: pugins 有哪些? grpc, gateway?

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/907835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号