array

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 1 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

array

Utility functions for working with Go slices, particularly for checking element existence.

Features

  • Generic functions that work with any comparable type
  • Efficient O(n) time complexity for slice operations
  • Zero external dependencies

Usage

Check if an element exists in a slice
package main

import (
    "fmt"
    "github.com/appleboy/com/array"
)

func main() {
    // Works with any comparable type
    intSlice := []int{1, 2, 3, 4, 5}
    exists := array.Contains(intSlice, 3)
    fmt.Println("Contains 3?", exists) // Output: Contains 3? true
    
    stringSlice := []string{"hello", "world", "go"}
    exists = array.Contains(stringSlice, "go")
    fmt.Println("Contains 'go'?", exists) // Output: Contains 'go'? true
    
    // Returns false if not found
    exists = array.Contains(intSlice, 99)
    fmt.Println("Contains 99?", exists) // Output: Contains 99? false
}

API Reference

Contains[T comparable](slice []T, key T) bool

Checks if a given key of any comparable type exists within a slice.

Parameters:

  • slice: A slice of any comparable type T
  • key: An element of type T to search for within the slice

Returns:

  • bool: True if the key is found in the slice, false otherwise

Time Complexity: O(n), where n is the length of the slice.

Notes:

  • Suitable for small to medium slices or infrequent lookups
  • For large slices with frequent lookups, consider using a map for better performance
  • Works with any comparable type (int, string, float64, etc.)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v0.3.0

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

Contains checks if a given key of any comparable type exists within a slice. It returns true if the key is found, otherwise it returns false.

Time complexity: O(n), where n is the length of the slice. Suitable for small to medium slices or infrequent lookups. For large slices with frequent lookups, consider using a map for better performance.

Parameters:

  • slice: A slice of any comparable type T.
  • key: An element of type T to search for within the slice.

Returns:

  • bool: True if the key is found in the slice, false otherwise.

Types

This section is empty.

Jump to

Keyboard shortcuts

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