Helper functions for the manipulation of slices of all types in Go

go-slices

Go Report Card Tweet

Unlike many other programming languages, Go doesn't provide helper functions for slices in it's core. I felt like this was quite an essential feature and there weren't any libraries out there that would do this, so I made my own. The functions are the same as the in the Go's official "strings" package, but you can use them on slices.

How to install

Open your terminal and type:

go get github.com/merkur0/go-slices

Then, import this package in your .go file like this:

import (
  "github.com/merkur0/go-slices"
)

And that's it!

Usage:

slices.FunctionName(parameters)

Example:

mySlice := []int{1, 2, 3}
slices.ContainsInt(mySlice, 3) // Returns "true"

List of functions:

Contains

Returns true if the slice contains the item and false if it doesn't. Example:

mySlice := []float32{1.1, 1.2, 1.3, 1.4, 1.5}
slices.ContainsFloat32(myArr, 1.2) // returns true
slices.ContainsFloat32(myArr, 2.3) // returns false

Count

Returns the number of instances of the item within the slice. Example:

mySlice := []int{1, 1, 1, 2, 1}
slices.CountInt(mySlice, 1) // returns 4
slices.CountInt(mySlice, 3) // returns 0

Delete

Returns a copy of the slice where the item with the index (second parameter) is deleted. Example:

mySlice := []int{1, 2, 3, 4, 5}
slices.DeleteInt(mySlice, 0) // returns [2, 3, 4, 5]

Index

Returns the index of the first instance of the second parameter in the slice, or -1 if item is not present in the slice. Example:

mySlice := []int{4, 1, 5}
slices.IndexInt(mySlice, 5) // returns 2

LastIndex

Returns the index of the last instance of the second parameter in the slice, or -1 if item is not present in the slice. Example:

mySlice := []int{1, 2, 1, 1, 4}
slices.LastIndexInt(mySlice, 1) // returns 3

Map

Returns a copy of the slice with all its items modified according to the mapping function. Example:

myFunc := func(number int) int {
    return number * 2
}
mySlice := []int{1, 2, 3, 4}

slices.MapInt(mySlice, myFunc) // returns [2, 4, 6, 8]

Max

Returns the largest element in the slice. Example:

mySlice := []int{1, 2, 3, 4}

slices.MaxInt(mySlice) // returns 4

Min

Returns the smallest element in the slice. Example:

mySlice := []int{1, 2, 3, 4}

slices.MinInt(mySlice) // returns 1

Pop

Returns a copy of the slice with it's last item removed. Example:

mySlice := []int{1, 2, 3, 4}
slices.PopInt(mySlice) // returns [1, 2, 3]

Replace

Returns a copy of the slice where the first instance of the old item (second parameter) is replaced with the new item (third parameter). Example:

mySlice := []int{1, 1, 3, 5, 1}
slices.ReplaceInt(mySlice, 1, 0) // returns [0, 1, 3, 5, 1]

ReplaceAll

Returns a copy of the slice where every instance of the old item (second parameter) is replaced with the new item (third parameter) and returns the new slice. Example:

mySlice := []int{1, 1, 3, 5, 1}
slices.ReplaceAllInt(mySlice, 1, 0) // returns [0, 0, 3, 5, 1]

Shift

Returns a copy of the slice with it's first item removed. Example:

mySlice := []int{1, 2, 3}
slices.ShiftInt(mySlice) // returns [2, 3]

Owner
Ondřej Merkun
Full Stack Developer at Coinrule
Ondřej Merkun
Similar Resources

mongodb helper functions, document and pipeline builder

MongoUtils Mongodb helper functions, document and pipeline builder. Helpers ParseObjectID Parse object id from string. ParseObjectID(id string) *primi

Dec 12, 2021

UltiTuner: a small helper tool to configure functions for Ultimaker S-Line printers

UltiTuner UltiTuner is a small helper tool to configure functions for Ultimaker

Apr 25, 2022

Helper functions for common scenarios, using Go generics.

zeroflucs generics When writing Go code for Go 1.17 or below, we've all written more than our fair share of methods to check "does this slice contain

Feb 18, 2022

Cookie - Cookie helper functions for golang

Cookie Cookie helper functions. package main import ( "net/http" "github.co

Feb 10, 2022

A selection of image manipulation tools

img A collection of image manipulation tools. Each tool takes an input file from standard input, this needs to be in PNG, JPEG or GIF format. They out

Dec 29, 2022

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration

Dataframes are used for statistics, machine-learning, and data manipulation/exploration. You can think of a Dataframe as an excel spreadsheet. This pa

Dec 31, 2022

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration

Dataframes are used for statistics, machine-learning, and data manipulation/exploration. You can think of a Dataframe as an excel spreadsheet. This pa

Jan 3, 2023

Easy Go GUI wrapper for interactive manipulation of visual algorithms/backend code.

Easy Go GUI wrapper for interactive manipulation of visual algorithms/backend code.

RenderView ================ Install: go get github.com/TheGrum/renderview Needs either Shiny (limited functionality), Gio, go-gtk, or gotk3. The latt

Aug 4, 2022

Swiss Army knife Proxy tool for HTTP/HTTPS traffic capture, manipulation, and replay on the go.

Swiss Army knife Proxy tool for HTTP/HTTPS traffic capture, manipulation, and replay on the go.

Features • Installation • Usage • Running Proxify • Installing SSL Certificate • Applications of Proxify • Join Discord Swiss Army Knife Proxy for rap

Jan 8, 2023

GUI application for Raspberry PI which allows simultaneous manipulation of infrared and nightvision cameras

IRNC GUI application for Raspberry PI which allows simultaneous manipulation of infrared and nightvision cameras IRNC stands for "InfraRed and Nightvi

Nov 25, 2021

Provides simple, semantic manipulation of the operating system's signal processing.

Provides simple, semantic manipulation of the operating system's signal processing.

Provides simple, semantic manipulation of the operating system's signal processing.

Dec 15, 2021

Common functional data manipulation and abstraction patterns in Golang.

Functional Patterns in Golang GOMAD (Early stage) This package is still in an early stage of development. Feel free to open a PR and contribute or jus

Jan 8, 2023

A color manipulation and conversion library for Go. 🌈 ✨

Khroma Khroma is a color manipulation and conversion library for Go. ✨ 🌈 Example package main import ( "log" "github.com/qbee-org/khroma" ) func

May 30, 2022

Some utility functions for generic types in Go.

GOUF - Utility Functions for generic types Go team released Go 1.18 beta recently with support for Generics(a.k.a type parameters). This package provi

Apr 13, 2022

A library containing useful functions for working with Go types.

Go Type Tools A library containing useful functions for working with Go types. Table of Contents Reasoning Examples Array Map Int String Usage License

Feb 18, 2022

Useful collection functions for golang, based on generic types

go-collection English | 简体中文 go-collection provides developers with a convenient set of functions for working with common slices, maps, and arrays dat

Jul 21, 2022

Go strcut based enum, support all types.

go-ultra-enum go-ultra-enum is an enum generator for Go. It is inspired by the powerful enum types found in Java. go-ultra-enum has the following capa

Dec 21, 2021

DeepCopy a portable app that allows you to copy all forms of specified file types from your entire file system of the computer

DeepCopy a portable app that allows you to copy all forms of specified file types from your entire file system of the computer

Dec 20, 2021

PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. You can use PHP like functions in your app, module etc. when you add this module to your project.

PHP Functions for Golang - phpfuncs PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. Y

Dec 30, 2022
Plugs module to see different types of plug types needed in different countries, and a comparison tool between two countries plug socket types.

plugs Importing the module: go get github.com/matthewboyd/plugs "github.com/matthewboyd/plugs" How to use the module: There are two functions wi

Dec 28, 2021
This package provides Go (golang) types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most famously used by app.diagrams.net, the new name of draw.io.

Go Draw - Golang MX This package provides types and helper functions to do some basic but useful things with mxGraph diagrams in XML, which is most fa

Aug 30, 2022
Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.

iter Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transformi

Dec 16, 2022
Scan database/sql rows directly to structs, slices, and primitive types

Scan Scan standard lib database rows directly to structs or slices. For the most comprehensive and up-to-date docs see the godoc Examples Multiple Row

Dec 28, 2022
Some helper types for go1: priority queue, slice wrapper.

go-villa Package villa contains some helper types for Go: priority queue, slice wrapper, binary-search, merge-sort. GoDoc Link: http://godoc.org/githu

Apr 24, 2021
Library providing opanapi3 and Go types for store/validation and transfer of ISO-4217, ISO-3166, and other types.

go-types This library has been created with the purpose to facilitate the store, validation, and transfer of Go ISO-3166/ISO-4217/timezones/emails/URL

Nov 9, 2022
Go linter that checks types that are json encoded - reports unsupported types and unnecessary error checks

Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omited.

Oct 7, 2022
Optional is a library that provides option types for Go types.

Option Optional is a library that provides option types for Go types. Installation Golang version 1.18 + required go get -u github.com/eatmoreapple/op

Nov 9, 2022
Go tool for generating sql scanners, sql statements and other helper functions

sqlgen generates SQL statements and database helper functions from your Go structs. It can be used in place of a simple ORM or hand-written SQL. See t

Nov 24, 2022
golang OpenGL helper functions

glh: golang OpenGL helpers This package contains a number of functions useful for applications using OpenGL. Code Reference Features Textures and Text

Apr 8, 2022