Skip to content

Instantly share code, notes, and snippets.

@wilk
Created November 8, 2017 19:58
Show Gist options
  • Save wilk/e6dcaf7493a356892dd71279f5dd0700 to your computer and use it in GitHub Desktop.
Save wilk/e6dcaf7493a356892dd71279f5dd0700 to your computer and use it in GitHub Desktop.
Goxfer
package main
import (
"fmt"
"strconv"
"gopkg.in/mgo.v2"
)
type Transaction struct {
Date string
Account string
Description string
Amount float64
Tags []string
}
var (
BULK_LEN, _ = strconv.Atoi(os.Getenv("BULK_LENGHT"))
)
func main() {
fmt.Println("Fetching transactions from DB...")
results := []Transaction{}
err = collected.Find(nil).All(&results)
if err != nil {
panic(err)
}
bulks := [][]Transaction{}
counter := 0
iterations := len(results) / BULK_LEN
for i := 0; i < iterations; i++ {
bulks = append(bulks, results[counter:counter + BULK_LEN])
counter += BULK_LEN
}
if counter < iterations {
bulks = append(bulks, results[counter:iterations - counter])
}
fmt.Println("Transactions fetched and divided into small bulk of #", strconv.Itoa(BULK_LEN), "transactions")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment