Last active
March 29, 2016 19:41
-
-
Save brydavis/eac3958b2c51d97b75f4 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
// heavily borrowed from Rosetta Code example | |
package main | |
import "fmt" | |
type row map[string]interface{} | |
var ( | |
tableA = []row{ | |
row{"name": "Jonah", "age": 27}, | |
row{"name": "Alan", "age": 18}, | |
row{"name": "Glory", "age": 28}, | |
row{"name": "Popeye", "age": 18}, | |
row{"name": "Alan", "age": 28}, | |
} | |
tableB = []row{ | |
row{"name": "Jonah", "fear": "Whales"}, | |
row{"name": "Jonah", "fear": "Spiders"}, | |
row{"name": "Alan", "fear": "Ghosts"}, | |
row{"name": "Alan", "fear": "Zombies"}, | |
row{"name": "Glory", "fear": "Buffy"}, | |
} | |
) | |
func main() { | |
Join(tableA, tableB, "name") | |
} | |
func Join(t1, t2 []row, on string) { | |
// hash phase | |
h := map[string][]interface{}{} | |
for _, r := range t1 { | |
key := r[on].(string) | |
val := fmt.Sprintf("%v", r["age"]) | |
h[key] = append(h[key], val) | |
} | |
// join phase | |
for _, r := range t2 { | |
key := r[on].(string) | |
for k, v := range r { | |
if k != on { | |
val := fmt.Sprintf("%v", v) | |
for _, a := range h[key] { | |
fmt.Println(key, a, val) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment