Last active
March 13, 2021 16:08
-
-
Save blkzy/c4cd10477c0b018a3aa2902ac7326104 to your computer and use it in GitHub Desktop.
Identifying what type of value the interface loads
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() { | |
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