Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "encoding/json"
- ms "github.com/mitchellh/mapstructure"
- )
- type Value struct {
- Key string
- OtherKey interface{}
- }
- type OtherKeyValue struct {
- OtherOtherKey string
- }
- func main() {
- // Construct value to marshal
- val := Value{Key: "yo", OtherKey: OtherKeyValue{OtherOtherKey: "yo2"}}
- fmt.Printf("Struct: %v\n", val)
- // Marshal
- b, err := json.Marshal(val)
- if err != nil {
- fmt.Printf("Error marshalling: %s\n", err)
- return
- }
- fmt.Printf("Marshalled: %s\n", string(b))
- // Unmarshal
- var toFill Value
- if err := json.Unmarshal(b, &toFill); err != nil {
- fmt.Printf("Error unmarshalling: %s\n", err)
- return
- }
- fmt.Printf("Unmarshalled: %v\n", toFill)
- // Cast
- var otherKeyValue OtherKeyValue
- ms.Decode(toFill.OtherKey, &otherKeyValue)
- fmt.Printf("Other key value: %v\n", otherKeyValue)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement