Goland中Protobuf的安裝、配置和使用
引言
本文記錄了mac環(huán)境下protobuf的編譯安裝,并通過一個(gè)示例來演示proto自動(dòng)生成go代碼。
本文使用的mac os 12.3系統(tǒng),不建議使用homebrew安裝,系統(tǒng)版本太高,會(huì)安裝報(bào)錯(cuò),所以自己下載新版壓縮包編譯構(gòu)建安裝。
1、安裝protobuf編譯器
在 官方github 選擇適合自己系統(tǒng)的Proto編譯器程序進(jìn)行下載安裝
本文使用的mac os 12.3系統(tǒng),不建議使用homebrew安裝,系統(tǒng)版本太高,會(huì)報(bào)錯(cuò),所以自己下載壓縮包構(gòu)建安裝。
- 下載地址:鏈接: https://pan.baidu.com/s/1NIMErRKrP3-DNmvA8SgKxg 提取碼: 27av
如需壓縮包請?jiān)谠u論區(qū)留言。
2.在/usr/local/下新建文件夾protobuf
3.將下載文件拷貝到:/usr/local/protobuf/
4.配置環(huán)境變量:
vim ~/.bash_profile
增加:
# protobuf export PROTOBUF=/usr/local/protobuf export PATH=$PROTOBUF/bin:$PATH
使環(huán)境變量生效:
source ~/.bash_profile
解壓:
tar zxvf protobuf-all-3.20.1.tar.gz
cd 進(jìn)入 protobuf-3.20.1/目錄下,在終端按順序執(zhí)行:
sudo ./configure sudo make sudo make check sudo make install
執(zhí)行命令:protoc --version 檢查是否安裝成功

2、下載protobuf的golang支持庫,安裝protoc-gen-go
protoc-gen-go用來將 .proto 文件轉(zhuǎn)換為 Golang 代碼。
在終端運(yùn)行命令:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
這條命令會(huì)安裝,并將protoc-gen-go可執(zhí)行文件復(fù)制到$GOBIN文件夾下

注意:原來的github.com/golang/protobuf/protoc-gen-go這個(gè)庫已經(jīng)被棄用,我們需要使用 google.golang.org/protobuf 這個(gè)庫
% go get -u github.com/golang/protobuf/protoc-gen-go go: module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead. go: added github.com/golang/protobuf v1.5.2 go: added google.golang.org/protobuf v1.28.0
3、protobuf使用示例
1、新建一個(gè)go moudle項(xiàng)目,創(chuàng)建擴(kuò)展名為.proto的文件,并編寫代碼。比如創(chuàng)建idl/user.proto文件,內(nèi)容如下:
syntax = "proto3";
package user;
option go_package ="./user";
message User {
int64 user_id = 1;
string user_name = 2;
string password = 3;
}2、編譯.proto文件,生成Go語言文件。執(zhí)行如下命令:
protoc --go_out=. ./idl/*.proto
將會(huì)自動(dòng)生成對應(yīng)的user目錄,存放生成的user.pb.go文件:

3、在main程序中使用Protobuf生成的代碼:
使用proto將user序列化輸出out,在將out反序列化成user
package main
import (
"encoding/json"
"fmt"
"github.com/starine/go-protoc-example/user"
"google.golang.org/protobuf/proto"
"log"
)
func main() {
fmt.Println("Hello World. \n")
user1 := user.User{}
user1.Password = "123456"
user1.UserName = "starine"
bytes, _ := json.Marshal(user1)
fmt.Println(string(bytes))
//序列化user結(jié)構(gòu)體數(shù)據(jù)
out, err := proto.Marshal(&user1)
if err != nil {
log.Fatalln("Failed to encode User:", err)
}
fmt.Println(out)
//反序列化user結(jié)構(gòu)體
user2 := user.User{}
err = proto.Unmarshal(out, &user2)
if err!=nil {
log.Fatalln("Failed to parse User:", err)
}
bytes, _ = json.Marshal(user2)
fmt.Println(string(bytes))
}運(yùn)行結(jié)果:
% go run main.go
Hello World.{"user_name":"starine","password":"123456"}
[18 7 115 116 97 114 105 110 101 26 6 49 50 51 52 53 54]
{"user_name":"starine","password":"123456"}Process finished with the exit code 0
到此這篇關(guān)于Goland中Protobuf的安裝、配置和使用的文章就介紹到這了,更多相關(guān)Protobuf安裝使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用dep 配置golang 開發(fā)環(huán)境的操作方法
下面小編就為大家?guī)硪黄褂胐ep 配置golang 開發(fā)環(huán)境的操作方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Golang 數(shù)據(jù)庫操作(sqlx)和不定字段結(jié)果查詢
本文主要介紹了Golang 數(shù)據(jù)庫操作(sqlx)和不定字段結(jié)果查詢,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Golang信號量設(shè)計(jì)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Golang信號量設(shè)計(jì)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Go語言利用Unmarshal解析json字符串的實(shí)現(xiàn)
本文主要介紹了Go語言利用Unmarshal解析json字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
golang語言編碼規(guī)范的實(shí)現(xiàn)
這篇文章主要介紹了golang語言編碼規(guī)范的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Go中各種newreader和newbuffer的使用總結(jié)
這篇文章主要為大家詳細(xì)介紹了Go語言中各種newreader和newbuffer的使用的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解下2023-11-11

