Last active
August 29, 2015 14:24
-
-
Save nathanborror/70ca117a9b42c84dd683 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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