From f4796f9c1e1374945a5ef96056a61ced1d89fbf6 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Tue, 19 Dec 2023 11:30:26 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20Int64Fro?= =?UTF-8?q?mString()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro/microfunctions.go | 10 ++++++++++ micro/microfunctions_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/micro/microfunctions.go b/micro/microfunctions.go index 2eeace4d..b3b329f1 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -792,3 +792,13 @@ func DeleteEndSlash(Text string) string { return Otvet } + +// Int64FromString - возвращает int64 из строки +func Int64FromString(s string) (int64, error) { + var Otvet int64 + var err error + + Otvet, err = strconv.ParseInt(s, 10, 64) + + return Otvet, err +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 5219a2d0..e0bc7f4a 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -511,3 +511,38 @@ func TestDeleteEndSlash(t *testing.T) { }) } } + +func TestInt64FromString(t *testing.T) { + // Test converting a valid string to int64 + input1 := "12345" + expected1 := int64(12345) + result1, err1 := Int64FromString(input1) + if err1 != nil { + t.Errorf("Expected no error, but got: %v", err1) + } + if result1 != expected1 { + t.Errorf("Expected %d, but got: %d", expected1, result1) + } + + // Test converting an empty string to int64 + input2 := "" + expected2 := int64(0) + result2, err2 := Int64FromString(input2) + if err2 == nil { + t.Errorf("Expected error, but got: %v", err2) + } + if result2 != expected2 { + t.Errorf("Expected %d, but got: %d", expected2, result2) + } + + // Test converting an invalid string to int64 + input3 := "abc" + expected3 := int64(0) + result3, err3 := Int64FromString(input3) + if err3 == nil { + t.Error("Expected an error, but got none") + } + if result3 != expected3 { + t.Errorf("Expected %d, but got: %d", expected3, result3) + } +}