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?

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JarvanStack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值