mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
Added noop case
parent
6a3f6ccf06
commit
77e5dbb474
25
Switch.md
25
Switch.md
@ -197,4 +197,27 @@ func do(v interface{}) string {
|
|||||||
do(21) == "42"
|
do(21) == "42"
|
||||||
do("bitrab") == "rabbit"
|
do("bitrab") == "rabbit"
|
||||||
do(3.142) == "unknown"
|
do(3.142) == "unknown"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Noop case
|
||||||
|
|
||||||
|
Sometimes it useful to have cases that require no action. This can look confusing, because it can appear that both the noop case and the subsequent case have the same action, but isn't so.
|
||||||
|
|
||||||
|
```go
|
||||||
|
func pluralEnding(n int) string {
|
||||||
|
ending := ""
|
||||||
|
|
||||||
|
switch n {
|
||||||
|
case 1:
|
||||||
|
default:
|
||||||
|
ending = "s"
|
||||||
|
}
|
||||||
|
|
||||||
|
return ending
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Sprintf("foo%s\n", pluralEnding(1)) == "foo"
|
||||||
|
fmt.Sprintf("bar%s\n", pluralEnding(2)) == "bars"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user