go - sql.Rows 到字符串 slice

标签 go

我正在使用标准的 go sql 包与 AWS Athena 进行交互。
我的查询为每条记录返回一个 uuid(字符串)和一组电子邮件。

这是代码:

package main


import (
  "fmt"
  "database/sql"
  _ "github.com/segmentio/go-athena"
  _"encoding/json"
)

type Contact struct {
  userid string
  emails []string
}


func main() {
  fmt.Println("hello")
  db, err := sql.Open("athena", "db=example")
  if err != nil {
    panic(err)
  }
  rows, err := db.Query("SELECT userid, transform(value.emails, x -> x.value) from database LIMIT 10")
  // Returns 
  // Row 1: "abc-123", ["email1@gmail.com", "email2@gmail.com"] 
  // Row 2: "def-456", ["email3@gmail.com"]

  if err != nil {
    panic(err)
  }

  for rows.Next() {
    var contact Contact
    rows.Scan(&contact.userid, &contact.emails)
    fmt.Println(contact)
  }
}

但是,我在 for 循环中收到此错误:
panic: unknown type `array` with value [email1@gmail.com]

我对提到的 array 类型感到困惑,我无法理解错误。
如何将返回的电子邮件列表映射到 Contact 结构中的一段字符串?

最佳答案

Athena 支持结构化数据类型。例如结构数据类型数组:

Structural types

    ARRAY < data_type >

从您收到的消息中,我假设列 email 的类型为 ARRAY<VARCHAR> .另外segmentio/go-athena在不受支持的操作上出现 panic ,例如 Begin用于事务(Athena 不支持)。要读取 Go 数组中的数据,您必须输入一些逻辑。见 read "SELECT *" columns into []string in goRead a Postgresql array directly into a Golang Slice作为首发。正如您在 pq 驱动程序中看到的那样,读取数组的实现方式可能与仅扫描一行不同

关于go - sql.Rows 到字符串 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59876684/

相关文章:

dictionary - 无法在 Google App Engine 数据存储上的 go 中插入动态属性

Golang 结构的 XML 和 JSON 标签?

regex - MustCompile 中的 Golang 正则表达式引用(查找重复字符)

python - 在没有 App Engine 的情况下运行谷歌云端点

go - Alexa for Business 可以使用 OAuth 2.0 吗?

go - 使用 Golang、Revel 处理预检请求

go - 如何对支持多个版本的服务器的特定服务进行节俭调用

inheritance - 如何在golang中嵌入其他包的结构

golang POST 图像和文本字段,带有 multipart/form-data

go - go 语言的 emacs 自动完成