anys

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

README

anys

Go Reference Go Report Card

A Go package that provides utilities for working with slices of any type, simplifying type conversions between concrete types and any (interface{}).

Installation

go get github.com/goaux/anys

Overview

The anys package offers a collection of functions that make it easier to work with slices of various types, particularly when converting between concrete types and the any type. This is useful when interfacing with APIs that require a slice of any or when working with heterogeneous collections.

Features

  • Convert slices of specific types to slices of any
  • Convert slices of any back to slices of specific types
  • Apply conversion functions across slices
  • Append values from one slice type to another
  • Safely handle type conversions with zero value fallbacks

Usage

Converting a slice to any
import (
	"fmt"
	"github.com/goaux/anys"
)

func main() {
	ints := []int{1, 2, 3}

	// Convert a slice of int to a slice of any
	anySlice := anys.From(ints...)

	fmt.Println(anySlice) // Output: [1 2 3]
}
Converting a slice of any back to a specific type
import (
	"fmt"
	"github.com/goaux/anys"
)

func main() {
	anySlice := []any{1, 2, 3}

	// Convert a slice of any back to a slice of int
	intSlice := anys.BackTo[int](anySlice...)

	fmt.Println(intSlice) // Output: [1 2 3]
}
Mapping a function across a slice
import (
	"fmt"
	"strconv"
	"strings"
	"github.com/goaux/anys"
)

func main() {
	ints := []int{1, 2, 3}

	// Map each integer to its string representation
	strSlice := anys.Map(func(i int) string {
		return strconv.Itoa(i)
	}, ints...)

	result := strings.Join(strSlice, ",")
	fmt.Println(result) // Output: 1,2,3
}
Appending values of different types
import (
	"fmt"
	"github.com/goaux/anys"
)

func main() {
	a := []any{1, "two"}
	ints := []int{3, 4, 5}

	// Append a slice of int to a slice of any.
	// The following line would cause a compile-time error because we cannot
	// directly append a slice of int to a slice of any.
	// a = append(a, ints...)
	a = anys.Append(a, ints...)

	fmt.Println(a) // Output: [1 two 3 4 5]
}
Appending with type assertion
import (
	"fmt"
	"github.com/goaux/anys"
)

func main() {
	ints := []int{1, 2}
	values := []any{3, 4, "invalid"}

	// Append values that can be type-asserted to int
	result := anys.AppendBackTo[int](ints, values...)

	fmt.Println(result) // Output: [1 2 3 4 0]
	// Note: "invalid" cannot be asserted to int, so it becomes 0 (zero value)
}
Appending with conversion
import (
	"fmt"
	"github.com/goaux/anys"
)

func main() {
	strings := []string{"a", "b"}
	ints := []int{1, 2}

	// Append ints to strings with conversion
	result := anys.AppendMap(strings, func(i int) string {
		return fmt.Sprintf("x%d", i)
	}, ints...)

	fmt.Println(result) // Output: [a b x1 x2]
}

Documentation

Overview

Package anys provides utilities for working with slices of any type.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[S any](a []any, s ...S) []any

Append takes a slice of any and appends the provided values to it.

This function is particularly useful when you need to append elements from a slice of a specific type (e.g., []int) to a slice of type []any, which cannot be done directly using Go's built-in append function.

Example usage:

a := []any{1, "two"}
ints := []int{3, 4, 5}
// The following line would cause a compile-time error because
// we cannot directly append a slice of int to a slice of any.
// a = append(a, ints...)
// Instead, use the custom Append function from the 'anys' package:
a = anys.Append(a, ints...)
fmt.Println(a)
// Output: [1 two 3 4 5]
Example
package main

import (
	"fmt"

	"github.com/goaux/anys"
)

func main() {
	a := []any{1, "two"}
	ints := []int{3, 4, 5}
	// The following line would cause a compile-time error because we cannot
	// directly append a slice of int to a slice of any.
	// a = append(a, ints...)
	// Instead, use the custom Append function from the 'anys' package to achieve this:
	a = anys.Append(a, ints...)
	fmt.Println(a)
}
Output:

[1 two 3 4 5]

func AppendBackTo

func AppendBackTo[T, S any](t []T, s ...S) []T

AppendBackTo returns a slice of type T by appending values that have been type asserted to T from the provided arguments. If a value cannot be asserted to type T, the corresponding element is set to T's zero value.

func AppendMap

func AppendMap[T, S any](t []T, conv func(S) T, s ...S) []T

AppendMap returns a slice of type T by applying the conversion function to each provided value and appending the result to the given slice.

func BackTo

func BackTo[T, S any](s ...S) []T

BackTo returns a slice of type T by attempting to assert each element in the provided slice `s` to type T. If an element cannot be asserted to type T, it is set to the zero value of T.

This function is useful for converting a slice of any (interface{}) values back to their original type.

Example:

ii := []int{1, 2, 3}
aa := anys.From(ii...)
i2 := anys.BackTo[int](aa...) // Convert the slice of any back to a slice of int
Example
package main

import (
	"fmt"
	"reflect"

	"github.com/goaux/anys"
)

func main() {
	ii := []int{1, 2, 3}
	aa := anys.From(ii...)
	i2 := anys.BackTo[int](aa...)
	fmt.Println(reflect.DeepEqual(ii, i2))
}
Output:

true

func From

func From[S any](s ...S) []any

From returns a slice of type `any` containing the provided values.

The From function is a utility that converts a slice of any type into a slice of `any`. This is particularly useful when you need to work with a homogeneous collection of elements of different types or when interfacing with APIs that require a slice of `any`.

Example:

ii := []int{1, 2, 3}
aa := anys.From(ii...) // Converts the slice of integers into a slice of any
Example
package main

import (
	"fmt"
	"reflect"

	"github.com/goaux/anys"
)

func main() {
	ints := []int{3, 4, 5}
	x := anys.From(ints...) // From is equivalent to the special case of Map
	y := anys.Map(func(i int) any { return any(i) }, ints...)
	fmt.Println(reflect.DeepEqual(x, y))
}
Output:

true

func Map

func Map[T, S any](conv func(S) T, s ...S) []T

Map returns a slice of type T obtained by applying the conversion function to each element of the input values.

Example
package main

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/goaux/anys"
)

func main() {
	ii := []int{1, 2, 3}
	s := strings.Join(
		anys.Map(func(i int) string { return strconv.Itoa(i) }, ii...),
		",",
	)
	fmt.Println(s)
}
Output:

1,2,3

Types

This section is empty.

Jump to

Keyboard shortcuts

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