github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/types/convert/ints.go (about)

     1  package convert
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/go-board/x-go/types"
     7  )
     8  
     9  func IntSliceToInt64Slice(a types.IntSlice) types.Int64Slice {
    10  	b := make(types.Int64Slice, len(a))
    11  	for i, x := range a {
    12  		b[i] = int64(x)
    13  	}
    14  	return b
    15  }
    16  
    17  func Int64SliceToIntSlice(a types.Int64Slice) types.IntSlice {
    18  	b := make(types.IntSlice, len(a))
    19  	for i, x := range a {
    20  		b[i] = int(x)
    21  	}
    22  	return b
    23  }
    24  
    25  func IntSliceToAnySlice(a types.IntSlice) types.AnySlice {
    26  	any := make(types.AnySlice, len(a))
    27  	for i, x := range a {
    28  		any[i] = x
    29  	}
    30  	return any
    31  }
    32  
    33  func Int64SliceToAnySlice(a types.Int64Slice) types.AnySlice {
    34  	b := make(types.AnySlice, len(a))
    35  	for i, x := range a {
    36  		b[i] = x
    37  	}
    38  	return b
    39  }
    40  
    41  func IntSliceToStringSlice(a types.IntSlice) types.StringSlice {
    42  	b := make(types.StringSlice, len(a))
    43  	for i, x := range a {
    44  		b[i] = strconv.Itoa(x)
    45  	}
    46  	return b
    47  }
    48  
    49  func Int64SliceToStringSlice(a types.Int64Slice) types.StringSlice {
    50  	b := make(types.StringSlice, len(a))
    51  	for i, x := range a {
    52  		b[i] = strconv.FormatInt(x, 10)
    53  	}
    54  	return b
    55  }
    56  
    57  func StringSliceToIntSlice(a types.StringSlice) (b types.IntSlice, hasErr bool) {
    58  	for _, x := range a {
    59  		i, err := strconv.Atoi(x)
    60  		if err != nil {
    61  			hasErr = true
    62  			continue
    63  		}
    64  		b = append(b, i)
    65  	}
    66  	return
    67  }
    68  
    69  func StringSliceToInt64Slice(a types.StringSlice) (b types.Int64Slice, hasErr bool) {
    70  	for _, x := range a {
    71  		i, err := strconv.ParseInt(x, 10, 64)
    72  		if err != nil {
    73  			hasErr = true
    74  			continue
    75  		}
    76  		b = append(b, i)
    77  	}
    78  	return
    79  }