A select
is only used with channels. Example
A switch
is used with concrete types. Example
A select
will choose multiple valid options at random, while aswitch
will go in sequence (and would require a fallthrough to match multiple.)
Note that a switch can also go over types for interfaces when used with the keyword .(type)
var a interface{}
a = 5
switch a.(type) {
case int:
fmt.Println("an int.")
case int32:
fmt.Println("an int32.")
}
// in this case it will print "an int."
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…