Initial commit
This commit is contained in:
45
utils/shortcuts.go
Normal file
45
utils/shortcuts.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func TryGetEnv[T any]( name string, fallback T ) T {
|
||||
v := os.Getenv( name )
|
||||
|
||||
if v != "" {
|
||||
switch any( fallback ).(type) {
|
||||
case uint64:
|
||||
p, err := strconv.ParseUint( v, 10, 64 )
|
||||
if err == nil {
|
||||
return any( uint64( p ) ).(T)
|
||||
}
|
||||
case uint32:
|
||||
p, err := strconv.ParseUint( v, 10, 32 )
|
||||
if err == nil {
|
||||
return any( uint32( p ) ).(T)
|
||||
}
|
||||
case int:
|
||||
p, err := strconv.ParseInt( v, 10, 32 )
|
||||
if err == nil {
|
||||
return any( int( p ) ).(T)
|
||||
}
|
||||
case float64:
|
||||
p, err := strconv.ParseFloat( v, 64 )
|
||||
if err == nil {
|
||||
return any( float64( p ) ).(T)
|
||||
}
|
||||
case float32:
|
||||
p, err := strconv.ParseFloat( v, 32 )
|
||||
if err == nil {
|
||||
return any( float32( p ) ).(T)
|
||||
}
|
||||
default:
|
||||
return any( v ).(T)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user