mapx

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: BSD-3-Clause Imports: 0 Imported by: 0

README


This utility package provides generic functions for map operations.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find[M ~map[T]E, T, E comparable](m M, v E) (T, bool)

Find searches for the first key in the map `m` that matches the value `v`. It returns the key and a boolean indicating whether the value was found.

- M is a map type where the key is of type T and the value is of type E. - T represents the key type, E represents the value type.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/abc/mapx"
)

func main() {
	m := map[string]int{
		"first":  1,
		"second": 2,
		"third":  3,
	}

	// Find the key for the value 2
	key, found := mapx.Find(m, 2)
	if found {
		fmt.Println("Found key:", key)
	} else {
		fmt.Println("Value not found")
	}

	// Find a non-existing value
	key, found = mapx.Find(m, 4)
	if !found {
		fmt.Println("Value not found")
	}

}
Output:

Found key: second
Value not found

Types

This section is empty.

Jump to

Keyboard shortcuts

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