add a helper function (#443)

This commit is contained in:
UUBulb
2024-10-21 12:11:02 +08:00
committed by GitHub
parent aa20c97312
commit cf5408751e
4 changed files with 21 additions and 6 deletions

View File

@@ -5,8 +5,11 @@ import (
"math/big"
"os"
"regexp"
"strconv"
"strings"
"golang.org/x/exp/constraints"
jsoniter "github.com/json-iterator/go"
)
@@ -104,3 +107,14 @@ func IfOrFn[T any](a bool, x, y func() T) T {
}
return y()
}
func Itoa[T constraints.Integer](i T) string {
switch any(i).(type) {
case int, int8, int16, int32, int64:
return strconv.FormatInt(int64(i), 10)
case uint, uint8, uint16, uint32, uint64:
return strconv.FormatUint(uint64(i), 10)
default:
return ""
}
}