cast

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 1 Imported by: 0

README

Cast

test Go Reference

Set of generic helpers for converting slices to their underlying type.

It's fast and allocs free when purego build tag is disabled.

Supported types
string
uint8
uint16
uint32
uint64
int8
int16
int32
int64
float32
float64
complex64
complex128
int
uint

Example

package main

import (
	"fmt"

	"github.com/WinPooh32/cast"
)

type MyString string

func show(ss []string) {
	fmt.Println(ss)
}

func main() {
	mm := []MyString{"hello", "world", "list", "of", "strings"}

	ss := cast.Strings(mm)

	show(ss)
}

Output:

[hello world list of strings]

Benchmarks

goos: linux
goarch: amd64
pkg: github.com/WinPooh32/cast
cpu: AMD Ryzen 7 3700X 8-Core Processor             
Benchmark_Strings-16            559789944  2.183 ns/op   0 B/op   0 allocs/op
Benchmark_castStringsAppend-16  20688170   57.98 ns/op   80 B/op  1 allocs/op
PASS
ok      github.com/WinPooh32/cast       2.700s

castStringsAppend implementation:

func castStringsAppend[T ~string](tt []T) []string {
	if tt == nil {
		return nil
	}
	vv := make([]string, 0, len(tt))
	for _, value := range tt {
		vv = append(vv, string(value))
	}
	return vv
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bools

func Bools[T ~bool](tt []T) []bool

func Bytes

func Bytes[T ~byte](tt []T) []byte

func Complex128s

func Complex128s[T ~complex128](tt []T) []complex128

func Complex64s

func Complex64s[T ~complex64](tt []T) []complex64

func Float32s

func Float32s[T ~float32](tt []T) []float32

func Float64s

func Float64s[T ~float64](tt []T) []float64

func Int16s

func Int16s[T ~int16](tt []T) []int16

func Int32s

func Int32s[T ~int32](tt []T) []int32

func Int64s

func Int64s[T ~int64](tt []T) []int64

func Int8s

func Int8s[T ~int8](tt []T) []int8

func Ints

func Ints[T ~int](tt []T) []int

func Strings

func Strings[T ~string](tt []T) []string
Example
type MyString string

show := func(ss []string) {
	fmt.Println(ss)
}

mm := []MyString{"hello", "world", "list", "of", "strings"}
ss := Strings(mm)

show(ss)
Output:

[hello world list of strings]

func Uint16s

func Uint16s[T ~uint16](tt []T) []uint16

func Uint32s

func Uint32s[T ~uint32](tt []T) []uint32

func Uint64s

func Uint64s[T ~uint64](tt []T) []uint64

func Uint8s

func Uint8s[T ~uint8](tt []T) []uint8

func Uints

func Uints[T ~uint](tt []T) []uint

Types

This section is empty.

Jump to

Keyboard shortcuts

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