From df11d6522dc279e241dcb9937594b93cc05766f0 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 21 Feb 2023 20:31:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=BD=ACint?= =?UTF-8?q?=E6=97=A0=E9=BB=98=E8=AE=A4=E5=80=BC=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/strings/strings.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/helper/strings/strings.go b/helper/strings/strings.go index 3df5779..51f30b5 100644 --- a/helper/strings/strings.go +++ b/helper/strings/strings.go @@ -32,6 +32,17 @@ func ToInteger[T constraints.Integer](s string, defaults T) T { } return T(i) } +func ToInt[T constraints.Integer](s string) T { + defaults := T(0) + if s == "" { + return defaults + } + i, err := strconv.ParseInt(s, 10, 64) + if err != nil { + return defaults + } + return T(i) +} func Md5(str string) string { h := md5.New()