Skip to content

Instantly share code, notes, and snippets.

@roustem
Created August 27, 2016 15:25
Show Gist options
  • Save roustem/d99b1d7185d13872d5e0c3bff638d130 to your computer and use it in GitHub Desktop.
Save roustem/d99b1d7185d13872d5e0c3bff638d130 to your computer and use it in GitHub Desktop.
Type conversion (with and without panic)
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