go_import_excel_pg/util/common.go
2024-03-06 09:15:08 +07:00

63 lines
1.0 KiB
Go

package util
import (
"fmt"
"log"
"reflect"
"strconv"
"strings"
)
const TIME_PARSE_LAYOUT = "2/1/2006"
const NilIntVal = int8(0)
func StringToInt[T int8 | int32 | int16 | int64](s string) T {
log.Println(s)
var val T
if strings.TrimSpace(s) == "" {
return 0
}
switch any(val).(type) {
case int8, int16, int32, int64:
num, _ := strconv.ParseInt(s, 10, 64)
switch any(val).(type) {
case int8:
val := int8(num)
return any(val).(T)
case int16:
val := int16(num)
return any(val).(T)
case int32:
val := int32(num)
return any(val).(T)
case int64:
val := int64(num)
return any(val).(T)
default:
return 0
}
default:
fmt.Printf("Unsupported type: %s\n", reflect.TypeOf(val).Name())
return 0
}
}
func StringToStringPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
func PadWithZeros(number int, zerosCount int) string {
numStr := strconv.Itoa(number)
padding := ""
for i := 0; i < zerosCount; i++ {
padding += "0"
}
return padding + numStr
}