Combinatoric is a simple Go port of the "combinatoric" parts of Python's
itertools
--specifically, combinations
, permutations
, and product
.
See godocs for more.
$ go get https://github.com/ecooper/combinatoric
package main
import (
"fmt"
"github.com/ecooper/combinatoric"
)
func main() {
src := []interface{}{"A", "B", "C", "D"}
// Create a new CombinationIterator of 2 elements using src
iter, _ := combinatoric.Combinations(src, 2)
// Print the length of the iterator
fmt.Printf("Expecting %d combinations:\n", iter.Len())
// Set c to the next combination until Next returns a nil slice.
for c := iter.First(); c != nil; c = iter.Next() {
fmt.Printf("%s\n", c)
}
// Restore the CombinationsIterator to its original state
iter.Reset()
}
See godocs for more documentation and usage examples.