riandyrn / go-strings-segmenter

Break slice of strings in Golang to segments iteratively

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Strings Segmenter

This is library to break slice of strings into segments iteratively.

For example we have following slice of strings:

strs := []string{"0", "1", "2", "3", "4"}

We want to break this slice of strings into segment that has maximum length of 2. So we would have following segments iteratively:

[]string{"0", "1"} // first iteration
[]string{"2", "3"} // second iteration
[]string{"4"} // third iteration

We could use this library to do just that.

Sample Usage

func main() {
    sgmntr := segmenter.NewSegmenter(segmenter.Configs{
        Strings:       []string{"0", "1", "2", "3", "4"},
        SegmentLength: 2
    })
    for sgmntr.HasNext() {
        log.Println(sgmntr.Next())
    }
    // The result would be:
    //
    // []string{"0", "1"}
    // []string{"2", "3"}
    // []string{"4"}
}

For more examples on usage please check segmenter_test.go.

Back to Top


About

Break slice of strings in Golang to segments iteratively


Languages

Language:Go 100.0%