dongyao2001 2019-01-04 06:22
浏览 36

如何使用sqlx获取一片字符串?

I am new to glang and I'd like to query Mysql using sqlx to get a slice of strings to be shipped as JSON. I know how to get the results like this:

type MediaURI struct {
    URI string `db:"uri" json:"uri"`
}


func MediaHandler(c *gin.Context) {
    var err error
    pid := c.PostForm("pid")
    var medias []MediaURI
    err = shared.Dbmap.Select(&medias, "SELECT uri FROM media WHERE post_id = ? ORDER BY created_at DESC ", pid)
    if err != nil {
        log.Println(err)
        return
    }
    c.JSON(http.StatusOK, gin.H{"mediaUrls": medias})
}

But this gives a slice of structs like this:

medias are:[{/media/photos/55/abc.png} {/media/photos/55/def.jpg} {/media/photos/55/gdx.png}]

I'm wondering how can I directly get the strings? I've looked at the docs but the answer (if there) is not clear to me.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥25 关于##爬虫##的问题,如何解决?:
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误