From 77e5dbb47415e1a2f288e9016afe6b1520e44b9c Mon Sep 17 00:00:00 2001 From: Ivan Vazquez Date: Thu, 30 Mar 2017 14:20:45 -0700 Subject: [PATCH] Added noop case --- Switch.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Switch.md b/Switch.md index a3e4dac9..50cf86bd 100644 --- a/Switch.md +++ b/Switch.md @@ -197,4 +197,27 @@ func do(v interface{}) string { do(21) == "42" do("bitrab") == "rabbit" do(3.142) == "unknown" -``` \ No newline at end of file +``` + +## 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" + +``` + \ No newline at end of file