Slices: Functions That Operate on Slices


Functions that operate on slices. Similar to functions from package strings or package bytes that have been adapted to work with slices.


  •  Using a thin layer of idiomatic Go; correctness over performance.
  •  Provide most basic slice operations: index, trim, filter, map
  •  Some PHP favorites like: pop, push, shift, unshift, shuffle, etc...
  •  Non-destructive returns (won't alter original slice), except for explicit tasks.

Quick Start

Install using "go get":

go get

Then import from your source:

import ""

View example_test.go for examples of basic usage and features.


This is a en example showing basic usage.

package main



func main() {
    str := `Don't communicate by sharing memory - share memory by communicating`

    // Split string by spaces into a slice.
    slc := strings.Split(str, " ")

    // Count the number of "memory" strings in slc.
    memories := slices.Count(slc, "memory")
    fmt.Println("Memories:", memories)

    // Split slice into two parts.
    parts := slices.Split(slc, "-")
    fmt.Println("Split:", parts, len(parts))

    // Compare second parts slice with original slc.
    diff := slices.Diff(slc, parts[1])
    fmt.Println("Diff:", diff)

    // Chunk the slice
    chunks := slices.Chunk(parts[0], 1)
    fmt.Println("Chunk:", chunks)

    // Merge the parts
    merge := slices.Merge(chunks...)
    fmt.Println("Merge:", merge)


The full code documentation is located at GoDoc:

Download Details:

Author: srfrog
Source Code: 
License: MIT license

#go #golang #functions 

Slices: Functions That Operate on Slices
1.00 GEEK