Skip to content
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

docs: 完善中文文档 #12

Merged
merged 1 commit into from
Aug 10, 2023
Merged
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
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ func Return[T any](boolExpression bool, trueReturnValue, falseReturnValue T) T {
return falseReturnValue
}
}

// ReturnByFunc
//
// @Description: if实现的三元表达式
// @param boolExpression: 布尔表达式,最终返回一个布尔值
// @param trueReturnValue: 当boolExpression返回值为true的时候执行此函数并返回值
// @param falseReturnValue: 当boolExpression返回值为false的时候执行此函数并返回值
// @return bool: 三元表达式的结果,为trueReturnValue或者falseReturnValue中的一个
func ReturnByFunc[T any](boolExpression bool, trueFuncForReturnValue, falseFuncForReturnValue func() T) T {
if boolExpression {
return trueFuncForReturnValue()
} else {
return falseFuncForReturnValue()
}
}
```

# 三、 Example
Expand Down Expand Up @@ -91,3 +106,30 @@ func main() {

```

或者使用函数进行返回,仅有被命中分支的函数才会得到执行,不过这种方式并不简洁并不建议使用:

```go
package main

import (
"fmt"
if_expression "github.com/golang-infrastructure/go-if-expression"
)

func main() {

r := if_expression.ReturnByFunc(true, func() string {
fmt.Println("True分支被执行了")
return "是"
}, func() string {
fmt.Println("False分支被执行了")
return "否"
})
fmt.Println(r)
// Output:
// True分支被执行了
// 是

}
```