Skip to content

Instantly share code, notes, and snippets.

@kentquirk
Last active September 11, 2016 03:56
Show Gist options
  • Save kentquirk/ccd5ec19e2fc7465da48c4d536875d59 to your computer and use it in GitHub Desktop.
Save kentquirk/ccd5ec19e2fc7465da48c4d536875d59 to your computer and use it in GitHub Desktop.
Go implementation with non-JSON input
package main
import "fmt"
type Pet struct {
Owner string `json:"owner"`
Pet string `json:"pet"`
}
func main() {
input := []Pet{
{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"},
}
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