Created
August 27, 2016 15:25
-
-
Save roustem/d99b1d7185d13872d5e0c3bff638d130 to your computer and use it in GitHub Desktop.
Type conversion (with and without panic)
This file contains hidden or 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 test() interface{} { | |
return 123 | |
} | |
func main() { | |
// no panic | |
s1, ok := test().(string) | |
if ok { | |
fmt.Println("result 1: ", s1) | |
} else { | |
fmt.Println("not a string") | |
} | |
// panic | |
s2, _ := test().(string) | |
fmt.Println("result 2: ", s2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment