Skip to content

Dev #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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了.
}