protobuf中使用wrapValue类型
go get google/protobuf/wrappers.proto
- 用来解决"零值问题": 本质是结构体指针
syntax = "proto3;
package = "api";
option go_package="protobuf_demo/api";
import "google/protobuf/wrappers.proto";
message Book {
string title = 1;
string author = 2;
goolgle.protobuf.Int64Value price = 3;
goolgle.protobuf.StringValue memo = 5;
//goolgle.protobuf.DoubleValue price2 = 4;
}
package main
import "google.golang.org/protobuf/types/known/wrapperspb";
func wrapValue() {
book := api.Book{
Title: "哈哈哈",
Price: &wrapperspb.Int64Value{Value: 8888},
Memo: &wrapperspb.StringValue{Value: "哈哈哈"},
}
if book.Price == nil {
}else {
book.GetPrice().GetValue()
}
if book.GetMemo() != nil {
book.GetMemo().GetValue()
}
}