huxi
huxi

Reputation: 33

How to get a slice of strings using 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.

Upvotes: 1

Views: 1219

Answers (0)

Related Questions