mongodb - Golang Selecting fields from a struct array -


i array of users attribute id in document:

users := []backend.user{}  err := collection.find(bson.m{"channel_id": bson.objectidhex(chid)}).all(&users) if err != nil {   println(err) } 

which want send json response browser/client. however, user struct contains things ids , hahsed passwords don't want send back!

i looking @ using reflect package select fields of struct , putting them map[string]interface{} im not sure how array of users.

you can ignore struct fields while json.marshal.

package main  import (     "encoding/json"     "fmt" )  type user struct {     id   int    `json:"-"`     name string `json:"name"` }  type users []*user  func main() {      user := &users{         &user{1, "max"},         &user{2, "alice"},         &user{3, "dan"},     }      json, _ := json.marshal(user)      fmt.println(string(json)) } 

runnable example in play golang: http://play.golang.org/p/aec_tyxe3b

there useful part using tags in doc. same xml, it's more complicated obvious reasons.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -