首页 > 后端开发 > Golang > 正文

如何在golang中向非本地结构添加接口?

王林
发布: 2024-02-06 08:18:11
转载
1335人浏览过

如何在golang中向非本地结构添加接口?

问题内容

我使用 https://github.com/lib/pq 从 postgres 获取数据。 为了提取数据,我使用第三方结构,其中包含 protobuf timestamp 字段 https://pkg.go.dev/google.golang.org/protobuf/types/known/timestamppb#timestamp 所以 所以问题是让工作从 time.time 扫描到 timestamppb.timestamp

type stuff struct { //this struct non-local, this is from third-party package
  date *timestamppb.timestamp
}

stuff = stuff{}

scanerr := rows.scan(&stuff.date)
登录后复制

我尝试扫描结构实现 sql.scanner 接口。那很简单。我只是像这样实现 scan 功能:

type test struct {
}


func (t test) scan(src any) error {
//convert value
}
登录后复制

但它不适用于timestamppb.timestamp,因为它是非本地类型。 然后我尝试定义本地类型并执行相同的操作

type timestamppb timestamppb.timestamp

func (t timestamppb) scan(src any) error {
//convert value
}
登录后复制

但是这个技巧并没有奏效。此外,我有警告“‘scan’通过值传递锁:类型‘timestamppb’包含‘protoimpl.messagestate’包含‘sync.mutex’,即‘sync.locker’” 如果我为 timestamppb 指定指针,这将不起作用

func (t *timestamppb) scan(src any) error {
登录后复制

所以我想知道,如何制作 sql.scanner 的 timestamppb.timestamp 实例。可能吗?

更新 1 我尝试过的小例子

type TimestampPb timestamppb.Timestamp

type test struct { //third-party struct, I can't modify it
    date *timestamppb.Timestamp
}

func (s *Storage) test() {
    rows, _ := s.db.Query("SELECT \"test_date\" FROM \"test_table\" LIMIT 1")

    defer func() {
        if rows != nil {
            _ = rows.Close()
        }
    }()

    for rows.Next() {
        //var value *TimestampPb //works
        //var value TimestampPb //works
        //var value *timestamppb.Timestamp //doesn't work
        //testStruct := test{} //doesn't work

        //err := rows.Scan(&value) //scan to variable
        //err := rows.Scan(&testStruct.date) //scan to field in the struct
        if err != nil {
            log.Fatalln(err)
        }
    }
}

func (tpb *TimestampPb) Scan(src any) error {
    return nil
}
登录后复制

我想知道我是否可以将 case 与 teststruct 一起使用?


正确答案


pq 库不支持 *timestamppb.timestamp 作为时间戳类型。 请参阅文档中支持的日期类型

BibiGPT-哔哔终结者
BibiGPT-哔哔终结者

B站视频总结器-一键总结 音视频内容

BibiGPT-哔哔终结者 28
查看详情 BibiGPT-哔哔终结者

扫描时需要不同的类型。

立即学习go语言免费学习笔记(深入)”;

我通常通过在函数中使用辅助类型来做到这一点。例如:

type row struct {
  Date *timestamppb.Timestamp
  // ... more fields here
}

func Query() ([]row, error) {
    rows, err := s.db.Query(...)
  if err != nil {
    return nil, err
  }
  defer rows.Close()

  var rows []row
  for rows.Next() {
    type aux struct {
      Date time.Time
      // ... more fields here
    }
    var value aux
    if err := rows.Scan(&value); err != nil {
      return nil, err
    }
    rows = append(rows, row{
      Date: timestamppb.New(value.Date),
      // ... more fields here
    }
  }
  return rows, nil
}
登录后复制

以上就是如何在golang中向非本地结构添加接口?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号