Skip to content

Instantly share code, notes, and snippets.

@sugilog
Created January 4, 2015 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugilog/1f42eb690398782e8a8b to your computer and use it in GitHub Desktop.
Save sugilog/1f42eb690398782e8a8b to your computer and use it in GitHub Desktop.
switchの挙動
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