使用jsonpb.Marshaler的MarshalToString()或Marshal()方法时,将int64类型的字段转换成数字类型可以使用google.protobuf.Int64Value类型。具体实现代码如下:
package main
import (
"fmt"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/wrappers"
)
type Test struct {
Id *wrappers.Int64Value `json:"id,omitempty"`
}
func main() {
t := Test{Id: &wrappers.Int64Value{Value: 1234567890}}
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(&t)
if err != nil {
panic(err)
}
fmt.Println(s) // 输出:{"id":1234567890}
}
在上面的例子中,Test结构体中的Id字段是*wrappers.Int64Value类型,它包装了一个int64类型的值。当使用jsonpb.Marshaler的MarshalToString()或Marshal()方法进行序列化时,会自动将*wrappers.Int64Value类型的字段转换成数字类型。