Last active
September 11, 2016 03:55
-
-
Save kentquirk/a16940c52d1a6e01f13d64ca8d2a67a1 to your computer and use it in GitHub Desktop.
Go without creating a type for pets
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" | |
) | |
func main() { | |
data := bytes.NewBuffer([]byte(`[ | |
{"owner": "Kent", "pet": "Shiner"}, | |
{"owner": "Mary", "pet": "Pumpkin"}, | |
{"owner": "Mary", "pet": "Tasha"}, | |
{"owner": "Paige", "pet": "Sushi"}, | |
{"owner": "Sarah", "pet": "Shiner"}, | |
{"owner": "Sarah", "pet": "Mr. Wigglesworth"}, | |
{"owner": "Sarah", "pet": "Pupparoo"}, | |
{"owner": "Sarah", "pet": "Lucy"} | |
]`)) | |
var input []map[string]string | |
json.NewDecoder(data).Decode(&input) | |
output := make(map[string][]string) | |
for _, pair := range input { | |
output[pair["owner"]] = append(output[pair["owner"]], pair["pet"]) | |
} | |
json.NewEncoder(os.Stdout).Encode(output) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment