diff options
author | Michael Graf <michael.graf@sendgrid.com> | 2020-01-30 15:29:06 -0800 |
---|---|---|
committer | Michael Graf <michael.graf@sendgrid.com> | 2020-01-30 15:35:19 -0800 |
commit | a7242df7d7f49ee2a7440a5a10f203d64e481f24 (patch) | |
tree | 1c714aff4b115f2caaeda060fed68d60996e57ee | |
parent | 1ada1d35bb0d34afe41451c4921a7bb6c7ebe259 (diff) |
go/en: add type switch and multiple cases
-rw-r--r-- | go.html.markdown | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/go.html.markdown b/go.html.markdown index 4fc155b5..49f1ade4 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -12,6 +12,7 @@ contributors: - ["Alexej Friesen", "https://github.com/heyalexej"] - ["Clayton Walker", "https://github.com/cwalk"] - ["Leonid Shevtsov", "https://github.com/leonid-shevtsov"] + - ["Michael Graf", "https://github.com/maerf0x0"] --- Go was created out of the need to get work done. It's not the latest trend @@ -196,7 +197,7 @@ func learnFlowControl() { x := 42.0 switch x { case 0: - case 1: + case 1, 2: // Can have multiple matches on one case case 42: // Cases don't "fall through". /* @@ -208,6 +209,19 @@ func learnFlowControl() { default: // Default case is optional. } + + // Type switch allows switching on the type of something instead of value + var data interface{} + data = "" + switch c := data.(type) { + case string: + fmt.Println(c, "is a string") + case int64: + fmt.Printf("%d is an int64\n", c) + default: + // all other cases + } + // Like if, for doesn't use parens either. // Variables declared in for and if are local to their scope. for x := 0; x < 3; x++ { // ++ is a statement. |