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

如何在 Go 中将表数据写入嵌套结构

如何在 Go 中将表数据写入嵌套结构

Go
梦里花落0921 2022-10-24 09:46:41
概括我正在尝试将多个 postgres 表中的数据写入嵌套的 Go 结构,以便在我的 Web 应用程序中返回对 GET 请求的单个 json 响应。问题从 Go 最佳实践的角度来看,我声明嵌套结构的方式是合理的,还是有理由避免这种方法并以另一种方式来做?我在步骤 3中做错了什么来阻止我的代码工作?(我担心答案是“一切”)到目前为止我所拥有的我已经声明了我的结构type MainObject struct {    SubObjects []struct {        SpecificDetail string `json:"specific-detail"`                           }  `json:"sub-object"`    ...(other []structs)...}我已经从表中检索了行func getMainObjectHandler(w http.ResponseWriter, r *http.Request) {        ...(database connnection)...        MainObjectID := r.URL.Query().Get("moid")    if MainObjectID != "null" {        NewMainObject := MainObject{}        SubObjectDetail_rows, err := db.Query("SELECT specific_detail from the_table WHERE moid= '" + MainObjectID + "'")        if err != nil {            log.Fatalf("could not execute query: %v", err)        }                ...(other db.Query rows)...我尝试(但失败)将行数据构建到结构中。        for SubObjectDetail_rows.Next() {            SpecificDetail := NewMainObject.SubObject.SpecificDetail{}            SubObjectDetail_rows.Scan(&SpecificDetail)            SubObject = append(SubObject, SpecificDetail)        }        NewMainObject = append(MainObject, SubObject)        defer persona_rows.Close()最后,我设置了 Marshal 并写入。        NMOListBytes, err := json.Marshal(NewMainObject)        if err != nil {            fmt.Println(fmt.Errorf("Error: %v", err))            w.WriteHeader(http.StatusInternalServerError)            return        }        w.Write(NMOListBytes)
查看完整描述

1 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

首先,请在创建 SQL 查询时使用占位符以避免注入:


// db.Query("SELECT specific_detail from the_table WHERE moid= '" + MainObjectID + "'")  // not this

db.Query("SELECT specific_detail from the_table WHERE moid=?", MainObjectID)

除非您使用的框架像GORM您不能Scan()使用单个结构值。从文档:


Scan 将当前行中的列复制到 dest 指向的值中。dest 中的值数必须与 Rows 中的列数相同。


看起来您正在从数据库查询中提取 JSON,因为您只查询一列,因此您可能想要:


var bs []byte // get raw JSON bytes


err = SubObjectDetail_rows.Scan(&bs) 

if err != nil { /* always check errors */ }

然后将它们解组到您的结构中:


err = json.Unmarshal(bs, &SpecificDetail)

if err != nil { /* ... */ }


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

添加回答

举报

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