52. Go - Type Assertions for Interfaces
Assertion allows accessing exact types from an interface.
Using parenthesis and the type inside.
Example:
package main
import "fmt"
func main() {
var jar interface{} = 2000
x := jar.(int)
fmt.Println(x)
y, err := jar.(string)
if err {
fmt.Println("STRING FOUND: ", y)
} else {
fmt.Println("STRING NOT FOUND")
}
}
// using double variables allow checking if the type exists in an interface.
// if true, else
Comments
Post a Comment