From 701c1b8e5050b15079a1ac611c97748ad9c94eea Mon Sep 17 00:00:00 2001 From: guonaihong Date: Fri, 14 Oct 2022 17:38:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eif/ifelse=20=E4=BE=BF?= =?UTF-8?q?=E6=8D=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ifop/ifop.go | 16 ++++++++++++++++ ifop/ifop_test.go | 20 ++++++++++++++++++++ radix/radix.go | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 ifop/ifop.go create mode 100644 ifop/ifop_test.go diff --git a/ifop/ifop.go b/ifop/ifop.go new file mode 100644 index 0000000..233a667 --- /dev/null +++ b/ifop/ifop.go @@ -0,0 +1,16 @@ +package ifop + +// apache 2.0 guonaihong +func If[T any](cond bool, t T) (zero T) { + if cond { + return t + } + return +} + +func IfElse[T any](cond bool, ifVal T, elseVal T) T { + if cond { + return ifVal + } + return elseVal +} diff --git a/ifop/ifop_test.go b/ifop/ifop_test.go new file mode 100644 index 0000000..d6387d3 --- /dev/null +++ b/ifop/ifop_test.go @@ -0,0 +1,20 @@ +package ifop + +// apache 2.0 guonaihong +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIf(t *testing.T) { + a := "" + assert.Equal(t, If(len(a) == 0, "default"), "default") +} + +func TestIfElse(t *testing.T) { + a := "" + assert.Equal(t, IfElse(len(a) != 0, a, "default"), "default") + a = "hello" + assert.Equal(t, IfElse(len(a) != 0, a, "default"), "hello") +} diff --git a/radix/radix.go b/radix/radix.go index 758096e..76dea99 100644 --- a/radix/radix.go +++ b/radix/radix.go @@ -1,5 +1,6 @@ package radix +// apache 2.0 guonaihong import ( "strings" "unicode/utf8" @@ -231,6 +232,5 @@ func (r *Radix[V]) Delete(k string) { // 返回长度 func (r *Radix[V]) Len() int { - return r.length }