Skip to content

Instantly share code, notes, and snippets.

@xin053
Created July 4, 2019 08:16
Show Gist options
  • Save xin053/fc4e9a7cbcabc8becaefd17c8dc76506 to your computer and use it in GitHub Desktop.
Save xin053/fc4e9a7cbcabc8becaefd17c8dc76506 to your computer and use it in GitHub Desktop.
[go switch] go switch #go #switch
// 条件在 case 中
func unhex(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}
// 根据 case 值判断
func shouldEscape(c byte) bool {
switch c {
case ' ', '?', '&', '=', '#', '+', '%':
return true
}
return false
}
// 类型判断
var t interface{}
t = functionOfSomeType()
switch t := t.(type) {
default:
fmt.Printf("unexpected type %T\n", t) // %T prints whatever type t has
case bool:
fmt.Printf("boolean %t\n", t) // t has type bool
case int:
fmt.Printf("integer %d\n", t) // t has type int
case *bool:
fmt.Printf("pointer to boolean %t\n", *t) // t has type *bool
case *int:
fmt.Printf("pointer to integer %d\n", *t) // t has type *int
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment