slices

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package slices provides generic utility functions for working with slices. These functions are designed to be simple, efficient, and type-safe using Go generics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](slice []T, element T) bool

Contains returns true if the slice contains the given element. Uses comparable constraint for type-safe equality comparison.

Example
package main

import (
	"fmt"

	"github.com/andygeiss/cloud-native-utils/slices"
)

func main() {
	fmt.Println(slices.Contains([]int{1, 2, 3}, 2))
	fmt.Println(slices.Contains([]int{1, 2, 3}, 9))
}
Output:
true
false

func ContainsAll

func ContainsAll[T comparable](slice []T, elements []T) bool

ContainsAll returns true if the slice contains all of the given elements.

func ContainsAny

func ContainsAny[T comparable](slice []T, elements []T) bool

ContainsAny returns true if the slice contains any of the given elements.

func Copy

func Copy[T any](slice []T) []T

Copy returns a shallow copy of the slice.

func Filter

func Filter[T any](slice []T, predicate func(T) bool) []T

Filter returns a new slice containing only elements that satisfy the predicate.

Example
package main

import (
	"fmt"

	"github.com/andygeiss/cloud-native-utils/slices"
)

func main() {
	even := slices.Filter([]int{1, 2, 3, 4, 5, 6}, func(n int) bool { return n%2 == 0 })
	fmt.Println(even)
}
Output:
[2 4 6]

func First

func First[T any](slice []T) (T, bool)

First returns the first element of the slice and true, or zero value and false if empty.

Example
package main

import (
	"fmt"

	"github.com/andygeiss/cloud-native-utils/slices"
)

func main() {
	value, ok := slices.First([]string{"alpha", "beta"})
	fmt.Println(value, ok)

	// An empty slice reports false rather than panicking.
	_, ok = slices.First([]string{})
	fmt.Println(ok)
}
Output:
alpha true
false

func IndexOf

func IndexOf[T comparable](slice []T, element T) int

IndexOf returns the index of the first occurrence of element in the slice, or -1 if the element is not found.

func Last

func Last[T any](slice []T) (T, bool)

Last returns the last element of the slice and true, or zero value and false if empty.

func Map

func Map[T any, R any](slice []T, transform func(T) R) []R

Map transforms each element in the slice using the given function.

Example
package main

import (
	"fmt"

	"github.com/andygeiss/cloud-native-utils/slices"
)

func main() {
	lengths := slices.Map([]string{"go", "rust", "zig"}, func(s string) int { return len(s) })
	fmt.Println(lengths)
}
Output:
[2 4 3]

func Unique

func Unique[T comparable](slice []T) []T

Unique returns a new slice with duplicate elements removed. Preserves the order of first occurrence.

Example
package main

import (
	"fmt"

	"github.com/andygeiss/cloud-native-utils/slices"
)

func main() {
	fmt.Println(slices.Unique([]string{"a", "b", "a", "c", "b"}))
}
Output:
[a b c]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL