diff --git a/5-type-assertions-in-go/Makefile b/5-type-assertions-in-go/Makefile new file mode 100644 index 0000000..d476aff --- /dev/null +++ b/5-type-assertions-in-go/Makefile @@ -0,0 +1,2 @@ +all: + go run main.go diff --git a/5-type-assertions-in-go/main.go b/5-type-assertions-in-go/main.go new file mode 100644 index 0000000..0c32ee1 --- /dev/null +++ b/5-type-assertions-in-go/main.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + var i interface{} = "hello" + + s := i.(string) + fmt.Println(s) + + s, ok := i.(string) + fmt.Println(s, ok) + + f, ok := i.(float64) + fmt.Println(f, ok) + + f = i.(float64) // panic + fmt.Println(f) +}