conversion.go 673 字节
package tool

import "strconv"

func FromStringSlice[T comparable](strArray []string, converter func(string) (T, error)) ([]T, error) {
	result := make([]T, len(strArray))
	for i, v := range strArray {
		convertedValue, err := converter(v)
		if err != nil {
			return nil, err
		}
		result[i] = convertedValue
	}
	return result, nil
}

func StringToInt(s string) (int, error) {
	return strconv.Atoi(s)
}

func StringToInt64(s string) (int64, error) {
	value, err := strconv.Atoi(s)
	return int64(value), err
}

func StringToFloat64(s string) (float64, error) {
	return strconv.ParseFloat(s, 64)
}

func StringToBool(s string) (bool, error) {
	return strconv.ParseBool(s)
}