Created
January 4, 2015 13:53
-
-
Save sugilog/1f42eb690398782e8a8b to your computer and use it in GitHub Desktop.
switchの挙動
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
fmt.Println( "patternA" ) | |
patternA( 1 ) | |
fmt.Println( "patternB" ) | |
patternB( 1 ) | |
} | |
func patternA( condition int ) { | |
switch condition { | |
case zero(): | |
fmt.Println( "0" ) | |
case one(): | |
fmt.Println( "1" ) | |
case two(): | |
fmt.Println( "2" ) | |
case three(): | |
fmt.Println( "3" ) | |
default: | |
fmt.Println( "-" ) | |
} | |
} | |
func patternB( condition int ) { | |
switch condition { | |
case zero(): | |
fmt.Println( "0" ) | |
case one(): | |
fmt.Println( "1" ) | |
fallthrough | |
case two(): | |
fmt.Println( "2" ) | |
case three(): | |
fmt.Println( "3" ) | |
default: | |
fmt.Println( "-" ) | |
} | |
} | |
func zero() int { | |
fmt.Println( "zero called" ) | |
return 0 | |
} | |
func one() int { | |
fmt.Println( "one called" ) | |
return 1 | |
} | |
func two() int { | |
fmt.Println( "two called" ) | |
return 2 | |
} | |
func three() int { | |
fmt.Println( "three called" ) | |
return 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment