Skip to content

Commit

Permalink
Merge pull request #6 from golang-infrastructure/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
CC11001100 authored Nov 24, 2022
2 parents 772c817 + 05546b4 commit 226ba33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@
Golang中缺少三元表达式,就导致某些情况三元表达式一行就能搞定的事情到Golang里面就得写得很啰嗦,
这是无法忍受的,~~这个库就是借助大量自定义的if函数来实现类似三元表达式的功能~~,最新版本是基于泛型实现的。

使用此库之前:

```go
if a % 2 == 0 {
return "偶数"
} else {
return "奇数"
}
```

使用此库之后:

```go
return if_expression.Return(a % 2 == 0, "偶数", "奇数")
```

对比:

```diff
- if a % 2 == 0 {
- return "偶数"
- } else {
- return "奇数"
- }
+ return if_expression.Return(a % 2 == 0, "偶数", "奇数")
```

## 二、引入依赖

go get安装:
Expand Down
8 changes: 8 additions & 0 deletions if_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ func ExampleReturn() {
// Output:
// 是
}

func TestMap(t *testing.T) {
m := map[string]interface{}{
"foo": "bar",
}
//t.Log(m["bad"]) // nil
t.Log(Return(m["bad"] != nil, m["bad"], "aaa")) // ⚠️ 范型传nil进来就panic了.
}

0 comments on commit 226ba33

Please sign in to comment.