v2
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package convertor
|
||||
|
||||
import "strconv"
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ConvertInt[T int | int16 | int32 | int64](value string, defaultValue T) T {
|
||||
i, err := strconv.Atoi(value)
|
||||
@@ -9,3 +12,17 @@ func ConvertInt[T int | int16 | int32 | int64](value string, defaultValue T) T {
|
||||
}
|
||||
return T(i)
|
||||
}
|
||||
|
||||
func QueryInt[T int | int16 | int32 | int64](vars url.Values, key string, defaultValue T) T {
|
||||
v := vars.Get(key)
|
||||
if len(v) == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
i, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return T(i)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user