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) + } +}