Skip to content

Instantly share code, notes, and snippets.

@blkzy
Last active March 13, 2021 16:08
Show Gist options
  • Save blkzy/c4cd10477c0b018a3aa2902ac7326104 to your computer and use it in GitHub Desktop.
Save blkzy/c4cd10477c0b018a3aa2902ac7326104 to your computer and use it in GitHub Desktop.
Identifying what type of value the interface loads
package main
import "fmt"
func main() {
identifyType(true)
identifyType([]string{})
identifyType("Hello World")
identifyType(0)
identifyType([]int{})
identifyType([]bool{})
}
func identifyType(any interface{}) {
switch any.(type) {
case string:
fmt.Println("Type string")
case int:
fmt.Println("Type int")
case int64:
fmt.Println("Type int64")
case int32:
fmt.Println("Type int32")
case bool:
fmt.Println("Type bool")
case []string:
fmt.Println("Type slice string")
case []int:
fmt.Println("Type slice int")
default:
fmt.Println("Type not identify!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment