mysql - 如何在 Golang protobuf v3 结构中获取 time.Time?
问题描述
我现在github.com/golang/protobuf/ptypes/timestamp
在 protobuf 消息文件中使用 google time 包。
google.protobuf.Timestamp UpdateTime = 9;
但是该UpdateTime
属性*timestamp.Timestamp
在protoc编译后成为golang struct中的指针,它不是a time.Time
,我无法将这些属性保存到Mysql时间戳列中。
我能做些什么?
解决方案
time.Time
要从type 的 protobuf 字段中获取 a google.protobuf.Timestamp
,请使用在该类型上定义的AsTime
方法timestamppb.Timestamp
。
在您的数据库或任何其他需要 a 的位置生成插入调用time.Time
时,调用myMsg.UpdateTime.AsTime()
以获取所需的值(其中myMsg
是相关 Protobuf 消息类型实例的变量)。
此答案假定您正在使用包中定义的新 Protobuf APIv2 接口,google.golang.org/protobuf
或者您正在使用 APIv1 的 APIv2 兼容实现,github.com/golang/protobuf
在版本v1.20
或更高版本的包中定义。
许多项目仍在更新到这些版本。强烈建议您升级代码生成和工具链以从新功能中受益。
推荐阅读
- docker - nginx 容器在主机上找不到证书文件
- mysql - 如何在 SQL 中连接两个表以从一个表中获取名称并在查询中显示它们?
- c# - 如何在 dotnet core 中编写跨平台构建后事件
- php - mod_rewrite 中的可选参数(漂亮的 url)
- php - OneNote 使用 Microsoft Graph 在 Laravel 中创建页面时出错
- javascript - 从另一个已经存在的数组创建 ID 数组
- ruby - 检查一个元素是否存在于使用 %w() 创建的数组中,Ruby
- conda - conda 安装的二进制文件与 anaconda.org 上的不同
- intellij-idea - Intellij Idea 自动更改项目名称
- firebase - Firebase Google 登录:本地主机未列入白名单