Created
September 13, 2012 08:35
-
-
Save mrosset/3712902 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
) | |
type Person struct { | |
Name string | |
} | |
func main() { | |
var p Person | |
decode(&p) | |
} | |
func decode(v interface{}) { | |
b := []byte(`{"Name": "joe"}`) | |
err := json.NewDecoder(bytes.NewReader(b)).Decode(v) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
switch t := v.(type) { | |
case *Person: | |
fmt.Println("saving person", (v).(*Person)) | |
default: | |
err := fmt.Errorf("unknow type %s", t) | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment