Skip to content

Commit

Permalink
新增if/ifelse 便捷函数
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Oct 14, 2022
1 parent be60bf0 commit 701c1b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
16 changes: 16 additions & 0 deletions ifop/ifop.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions ifop/ifop_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion radix/radix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package radix

// apache 2.0 guonaihong
import (
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -231,6 +232,5 @@ func (r *Radix[V]) Delete(k string) {

// 返回长度
func (r *Radix[V]) Len() int {

return r.length
}

0 comments on commit 701c1b8

Please sign in to comment.