Merge pull request #3833 from MaerF0x0/master

go/en: add type switch and multiple cases
This commit is contained in:
Max Schumacher 2020-01-31 09:47:30 +01:00 committed by GitHub
commit 2c3f9298aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@ contributors:
- ["Alexej Friesen", "https://github.com/heyalexej"] - ["Alexej Friesen", "https://github.com/heyalexej"]
- ["Clayton Walker", "https://github.com/cwalk"] - ["Clayton Walker", "https://github.com/cwalk"]
- ["Leonid Shevtsov", "https://github.com/leonid-shevtsov"] - ["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 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 x := 42.0
switch x { switch x {
case 0: case 0:
case 1: case 1, 2: // Can have multiple matches on one case
case 42: case 42:
// Cases don't "fall through". // Cases don't "fall through".
/* /*
@ -208,6 +209,19 @@ func learnFlowControl() {
default: default:
// Default case is optional. // 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. // Like if, for doesn't use parens either.
// Variables declared in for and if are local to their scope. // Variables declared in for and if are local to their scope.
for x := 0; x < 3; x++ { // ++ is a statement. for x := 0; x < 3; x++ { // ++ is a statement.