Add code
This commit is contained in:
parent
6a315ca521
commit
b2fb9c90d4
2 changed files with 21 additions and 0 deletions
2
5-type-assertions-in-go/Makefile
Normal file
2
5-type-assertions-in-go/Makefile
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
all:
|
||||||
|
go run main.go
|
19
5-type-assertions-in-go/main.go
Normal file
19
5-type-assertions-in-go/main.go
Normal file
|
@ -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)
|
||||||
|
}
|
Reference in a new issue