Skip to content

Instantly share code, notes, and snippets.

@brydavis
Last active March 29, 2016 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brydavis/eac3958b2c51d97b75f4 to your computer and use it in GitHub Desktop.
Save brydavis/eac3958b2c51d97b75f4 to your computer and use it in GitHub Desktop.
// 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