gitee.com/quant1x/num@v0.3.2/type_integer.go (about)

     1  package num
     2  
     3  func integer2Bool[T Number](v T) bool {
     4  	if v != 0 {
     5  		return true
     6  	}
     7  	return false
     8  }
     9  
    10  // number类型切片转换
    11  func sliceNumberConvert[F Number, T Number](s []F) []T {
    12  	count := len(s)
    13  	if count == 0 {
    14  		return []T{}
    15  	}
    16  	d := make([]T, count)
    17  	for idx, iv := range s {
    18  		d[idx] = T(iv)
    19  	}
    20  	return d
    21  }
    22  
    23  // SliceToInts any输入只能是一维slice或者数组
    24  func SliceToInts(v any) []int {
    25  	switch values := v.(type) {
    26  	case []int8:
    27  		return sliceNumberConvert[int8, int](values)
    28  	case []uint8:
    29  		return sliceNumberConvert[uint8, int](values)
    30  	case []int16:
    31  		return sliceNumberConvert[int16, int](values)
    32  	case []uint16:
    33  		return sliceNumberConvert[uint16, int](values)
    34  	case []int32:
    35  		return sliceNumberConvert[int32, int](values)
    36  	case []uint32:
    37  		return sliceNumberConvert[uint32, int](values)
    38  	case []int64:
    39  		return sliceNumberConvert[int64, int](values)
    40  	case []uint64:
    41  		return sliceNumberConvert[uint64, int](values)
    42  	case []int:
    43  		return sliceNumberConvert[int, int](values)
    44  	case []uint:
    45  		return sliceNumberConvert[uint, int](values)
    46  	case []uintptr:
    47  		return sliceNumberConvert[uintptr, int](values)
    48  	case []float32:
    49  		return sliceNumberConvert[float32, int](values)
    50  	case []float64:
    51  		return sliceNumberConvert[float64, int](values)
    52  	case []bool:
    53  		count := len(values)
    54  		if count == 0 {
    55  			return []int{}
    56  		}
    57  		vs := make([]int, count)
    58  		for idx, iv := range values {
    59  			vs[idx] = AnyToGeneric[int](iv)
    60  		}
    61  		return vs
    62  	case []string:
    63  		count := len(values)
    64  		if count == 0 {
    65  			return []int{}
    66  		}
    67  		vs := make([]int, count)
    68  		for idx, iv := range values {
    69  			vs[idx] = AnyToGeneric[int](iv)
    70  		}
    71  		return vs
    72  	default:
    73  		panic(TypeError(values))
    74  	}
    75  	return []int{}
    76  }