go_import_excel_pg/util/common.go
2024-02-23 14:10:40 +07:00

57 lines
923 B
Go

package util
import (
"fmt"
"reflect"
"strconv"
)
const TIME_PARSE_LAYOUT = "2/1/2006"
const NilIntVal = int8(0)
func StringToIntPtr[T int8 | int32 | int64](s string) *T {
var val T
switch any(val).(type) {
case int8, int32, int64:
num, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil
}
switch any(val).(type) {
case int8:
val := int8(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 nil
}
default:
fmt.Printf("Unsupported type: %s\n", reflect.TypeOf(val).Name())
return nil
}
}
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
}