Skip to content
Utility class for handling slices
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.chglog
.gitignore
CHANGELOG.md
LICENSE
README.md
bool.go
bool_test.go
float32.go
float32_test.go
float64.go
float64_test.go
go.mod
int.go
int16.go
int16_test.go
int32.go
int32_test.go
int64.go
int64_test.go
int8.go
int8_test.go
int_test.go
interface.go
interface_test.go
logo.png
string.go
string_test.go

README.md

Utility class for handling slices.

Go Report Card GoDoc CodeFactor cover.run Mentioned in Awesome Go

Install

go get -u github.com/leaanthony/slicer

Quick Start

  import "github.com/leaanthony/slicer"

  func test() {
    s := slicer.String()
    s.Add("one")
    s.Add("two")
    s.AddSlice([]string{"three","four"})
    fmt.Printf("My slice = %+v\n", s.AsSlice())
    
    t := slicer.String()
    t.Add("zero")
    t.AddSlicer(s)
    fmt.Printf("My slice = %+v\n", t.AsSlice())
  }

Available slicers

  • Int
  • Int8
  • Int16
  • Int32
  • Int64
  • Float32
  • Float64
  • String
  • Bool
  • Interface

API

Construction

Create new Slicers by calling one of the following functions:

  • Int()
  • Int8()
  • Int16()
  • Int32()
  • Int64()
  • Float32()
  • Float64()
  • String()
  • Bool()
  • Interface()
  s := slicer.String()

If you wish to convert an existing slice to a Slicer, you may pass it in during creation:

  values := []string{"one", "two", "three"}
  s := slicer.String(values)

Add

Adds a value to the slice.

  values := []string{"one", "two", "three"}
  s := slicer.String(values)
  s.Add("four")

AddSlice

Adds an existing slice of values to a slicer

  s := slicer.String([]string{"one"})
  s.AddSlice([]string{"two"})

AsSlice

Returns a regular slice from the slicer.

  s := slicer.String([]string{"one"})
  for _, value := range s.AsSlice() {
    ...
  }

AddSlicer

Adds an existing slicer of values to another slicer

  a := slicer.String([]string{"one"})
  b := slicer.String([]string{"two"})
  a.AddSlicer(b)

Filter

Filter the values of a slicer based on the result of calling the given function with each value of the slice. If it returns true, the value is added to the result.

  a := slicer.Int([]int{1,5,7,9,6,3,1,9,1})
  result := a.Filter(func(v int) bool {
    return v > 5
  })
  // result is []int{7,9,9}
  

Each

Each iterates over all the values of a slicer, passing them in as paramter to a function

a := slicer.Int([]int{1,5,7,9,6,3,1,9,1})
  result := 0
  a.Each(func(v int) {
    result += v
  })
  // result is 42
You can’t perform that action at this time.