From 51de4fe688aba1afd60a055882271621107a8550 Mon Sep 17 00:00:00 2001 From: kkumar-gcc Date: Thu, 12 Sep 2024 13:10:13 +0530 Subject: [PATCH] solve potential nil error for str.When --- support/str/str.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/support/str/str.go b/support/str/str.go index 5714f729d..117bc067f 100644 --- a/support/str/str.go +++ b/support/str/str.go @@ -804,12 +804,14 @@ func (s *String) Upper() *String { } // When returns the String instance with the given callback applied if the given condition is true. -// If the condition is false, the fallback callback is applied.(if provided) +// If the condition is false, the fallback callback is applied (if provided). func (s *String) When(condition bool, callback ...func(*String) *String) *String { if condition { - return callback[0](s) + if len(callback) > 0 && callback[0] != nil { + return callback[0](s) + } } else { - if len(callback) > 1 { + if len(callback) > 1 && callback[1] != nil { return callback[1](s) } }