Skip to content

Commit

Permalink
Merge pull request #13 from golang-infrastructure/dev
Browse files Browse the repository at this point in the history
docs: 增加英文文档
  • Loading branch information
CC11001100 committed Aug 10, 2023
2 parents 019926a + e9ceb36 commit 67824bb
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Golang的三元表达式实现

[中文文档](./README.md) | [English Document](./README_en.md)

# 一、开发初衷

Golang中缺少三元表达式,就导致某些情况三元表达式一行就能搞定的事情到Golang里面就得写得很啰嗦,
Expand Down
136 changes: 136 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Golang's ternary expression implementation

[中文文档](./README.md) | [English Document](./README_en.md)

# 1. The original intention of development

The lack of ternary expressions in Golang has led to the fact that in some cases, things that can be done in one line with ternary expressions have to be written very verbosely in Golang.
This is unbearable, ~~this library uses a large number of custom if functions to achieve functions similar to ternary expressions~~, the latest version is implemented based on generics.

Before using this library:

```go
if a % 2 == 0 {
return "even number"
} else {
return "odd number"
}
```

After using this library:

```go
return if_expression.Return(a % 2 == 0, "even", "odd")
```

Compared:

``` diff
- if a % 2 == 0 {
- return "even number"
- } else {
- return "odd number"
- }
+ return if_expression.Return(a % 2 == 0, "even", "odd")
```

## 2. Introduce dependencies

go get install:

```text
go get -u github.com/golang-infrastructure/go-if-expression
```

If you don't want to add new dependencies, just copy the following code into your utils, the generic version is very concise:

```go
package if_expression

// Return
//
// @Description: The ternary expression implemented by if
// @param boolExpression: Boolean expression, finally returns a Boolean value
// @param trueReturnValue: The value returned when the return value of boolExpression is true
// @param falseReturnValue: The value returned when boolExpression returns false
// @return bool: The result of the ternary expression, which is one of trueReturnValue or falseReturnValue
func Return[T any](boolExpression bool, trueReturnValue, falseReturnValue T) T {
if boolExpression {
return trueReturnValue
} else {
return falseReturnValue
}
}

// ReturnByFunc
//
// @Description: The ternary expression implemented by if
// @param boolExpression: Boolean expression, finally returns a Boolean value
// @param trueReturnValue: When the return value of boolExpression is true, execute this function and return the value
// @param falseReturnValue: Execute this function and return value when boolExpression returns false
// @return bool: The result of the ternary expression, which is one of trueReturnValue or falseReturnValue
func ReturnByFunc[T any](boolExpression bool, trueFuncForReturnValue, falseFuncForReturnValue func() T) T {
if boolExpression {
return trueFuncForReturnValue()
} else {
return falseFuncForReturnValue()
}
}
```

# 3. Example

For example, the most common default value scenario:

```go
threadNum := 0
fmt.Printf("Number of threads: %d", if_expression.Return(threadNum == 0, 1, threadNum))
```

Example of use:

```go
package main

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

func main() {

r := if_expression.Return(true, "Yes", "No")
fmt.Println(r)
//Output:
// yes

}

```

Or use a function to return, only the function that is hit by the branch will be executed, but this method is not concise and is not recommended:

```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 branch is executed")
return "yes"
}, func() string {
fmt.Println("False branch is executed")
return "no"
})
fmt.Println(r)
//Output:
// True branch is executed
// yes

}
```

0 comments on commit 67824bb

Please sign in to comment.