go_import_excel_pg/util/common.go

63 lines
1.0 KiB
Go
Raw Normal View History

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