Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanborror/70ca117a9b42c84dd683 to your computer and use it in GitHub Desktop.
Save nathanborror/70ca117a9b42c84dd683 to your computer and use it in GitHub Desktop.
import (
"bytes"
"encoding/json"
)
// ToModel converts a string map to a given model
func ToModel(obj map[string]string, model interface{}) error {
j, _ := json.Marshal(obj)
d := json.NewDecoder(bytes.NewReader(j))
return d.Decode(model)
}
// Usage
type User struct {
name string `json:"name"`
email string `json:"email"`
}
var user User
reply, _ := redis.StringMap(c.Do("HGETALL", "user:1"))
err := ToModel(reply, &user)
if err != nil {
log.Println(err.Error())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment