为了账号安全,请及时绑定邮箱和手机立即绑定

使用 pgx 从 Postgres 扫描 PostGIS 点时,接口 {} 是字符串

使用 pgx 从 Postgres 扫描 PostGIS 点时,接口 {} 是字符串

Go
不负相思意 2022-06-27 10:43:31
我有一个buildings包含coordinate类型列的表的数据库GEOMETRY(POINT, 4326)。带着希望能读懂坐标,我写了如下代码:    rows, err := db.pool.Query(context.Background(), `select "uuid", "coordinate" from "building"`)    defer rows.Close()    for rows.Next() {        var uuid pgtype.UUID        var coordinate postgis.Point        err := rows.Scan(&uuid, &coordinate)        if err != nil {            return err        }        log.Println("~~~", coordinate.X, coordinate.Y)    }然后,我收到以下错误:2020/08/22 18:32:32 [Recovery] 2020/08/22 - 18:32:32 panic recovered:POST /get-state HTTP/1.1Host: localhost:3000Accept: */*Accept-Encoding: gzip, deflate, brConnection: keep-aliveContent-Length: 0Postman-Token: 4e6f4dc7-9dc4-4cd4-a328-8da2bc86d43dUser-Agent: PostmanRuntime/7.26.3interface conversion: interface {} is string, not []uint8/usr/local/Cellar/go/1.14.6/libexec/src/runtime/iface.go:260 (0x100bd11)    panicdottypeE: panic(&TypeAssertionError{iface, have, want, ""})/Users/virtumonde/go/pkg/mod/github.com/cridenour/go-postgis@v1.0.0/decode.go:21 (0x10fda31)    decode: ewkb, err := hex.DecodeString(string(value.([]byte)))/Users/virtumonde/go/pkg/mod/github.com/cridenour/go-postgis@v1.0.0/point.go:48 (0x10fde58)    (*Point).Scan: reader, err := decode(value)/Users/virtumonde/go/pkg/mod/github.com/jackc/pgtype@v1.4.2/pgtype.go:590 (0x120ff39)    scanPlanSQLScanner.Scan: return scanner.Scan(string(src))/Users/virtumonde/go/pkg/mod/github.com/jackc/pgx/v4@v4.8.1/rows.go:220 (0x1627b2c)    (*connRows).Scan: err := rows.scanPlans[i].Scan(ci, fieldDescriptions[i].DataTypeOID, fieldDescriptions[i].Format, values[i], dst)/Users/virtumonde/go/pkg/mod/github.com/jackc/pgx/v4@v4.8.1/pgxpool/rows.go:70 (0x1634cd4)    (*poolRows).Scan: err := rows.r.Scan(dest...)先感谢您。任何的意见都将会有帮助。
查看完整描述

1 回答

?
米脂

TA贡献1836条经验 获得超3个赞

import ("github.com/paulmach/orb/encoding/wkb")
err := rows.Scan(&uuid, wkb.Scanner(&coordinate))


查看完整回答
反对 回复 2022-06-27
  • 1 回答
  • 0 关注
  • 182 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信