Skip to content

Commit 617c04b

Browse files
committed
docs: 增加英文文档
1 parent 18a5914 commit 617c04b

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

README_en.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Golang's ternary expression implementation
2+
3+
# 1. The original intention of development
4+
5+
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.
6+
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.
7+
8+
Before using this library:
9+
10+
```go
11+
if a % 2 == 0 {
12+
return "even number"
13+
} else {
14+
return "odd number"
15+
}
16+
```
17+
18+
After using this library:
19+
20+
```go
21+
return if_expression.Return(a % 2 == 0, "even", "odd")
22+
```
23+
24+
Compared:
25+
26+
``` diff
27+
- if a % 2 == 0 {
28+
- return "even number"
29+
- } else {
30+
- return "odd number"
31+
- }
32+
+ return if_expression.Return(a % 2 == 0, "even", "odd")
33+
```
34+
35+
## 2. Introduce dependencies
36+
37+
go get install:
38+
39+
```text
40+
go get -u github.com/golang-infrastructure/go-if-expression
41+
```
42+
43+
If you don't want to add new dependencies, just copy the following code into your utils, the generic version is very concise:
44+
45+
```go
46+
package if_expression
47+
48+
// Return
49+
//
50+
// @Description: The ternary expression implemented by if
51+
// @param boolExpression: Boolean expression, finally returns a Boolean value
52+
// @param trueReturnValue: The value returned when the return value of boolExpression is true
53+
// @param falseReturnValue: The value returned when boolExpression returns false
54+
// @return bool: The result of the ternary expression, which is one of trueReturnValue or falseReturnValue
55+
func Return[T any](boolExpression bool, trueReturnValue, falseReturnValue T) T {
56+
if boolExpression {
57+
return trueReturnValue
58+
} else {
59+
return falseReturnValue
60+
}
61+
}
62+
63+
// ReturnByFunc
64+
//
65+
// @Description: The ternary expression implemented by if
66+
// @param boolExpression: Boolean expression, finally returns a Boolean value
67+
// @param trueReturnValue: When the return value of boolExpression is true, execute this function and return the value
68+
// @param falseReturnValue: Execute this function and return value when boolExpression returns false
69+
// @return bool: The result of the ternary expression, which is one of trueReturnValue or falseReturnValue
70+
func ReturnByFunc[T any](boolExpression bool, trueFuncForReturnValue, falseFuncForReturnValue func() T) T {
71+
if boolExpression {
72+
return trueFuncForReturnValue()
73+
} else {
74+
return falseFuncForReturnValue()
75+
}
76+
}
77+
```
78+
79+
# 3. Example
80+
81+
For example, the most common default value scenario:
82+
83+
```go
84+
threadNum := 0
85+
fmt.Printf("Number of threads: %d", if_expression.Return(threadNum == 0, 1, threadNum))
86+
```
87+
88+
Example of use:
89+
90+
```go
91+
package main
92+
93+
import (
94+
"fmt"
95+
if_expression "github.com/golang-infrastructure/go-if-expression"
96+
)
97+
98+
func main() {
99+
100+
r := if_expression.Return(true, "Yes", "No")
101+
fmt.Println(r)
102+
//Output:
103+
// yes
104+
105+
}
106+
107+
```
108+
109+
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:
110+
111+
```go
112+
package main
113+
114+
import (
115+
"fmt"
116+
if_expression "github.com/golang-infrastructure/go-if-expression"
117+
)
118+
119+
func main() {
120+
121+
r := if_expression. ReturnByFunc(true, func() string {
122+
fmt.Println("True branch is executed")
123+
return "yes"
124+
}, func() string {
125+
fmt.Println("False branch is executed")
126+
return "no"
127+
})
128+
fmt.Println(r)
129+
//Output:
130+
// True branch is executed
131+
// yes
132+
133+
}
134+
```

0 commit comments

Comments
 (0)