package main
import (
"fmt"
"reflect"
)
type IMatchTeam interface {
GetTeamIndex() uint32
}
type MatchTeam struct {
}
func (this *MatchTeam) GetTeamIndex() uint32 {
return 0
}
// this function return an interface
func ChangeTeam() IMatchTeam {
var t *MatchTeam
return t // "t" is nil, but Go wraps this "typed nil" into a interface object, which is not nil itself
}
func main() {
res := ChangeTeam() // res now is an interface object
if res != nil {
fmt.Println("This is true!")
ti, ok := res.(IMatchTeam)
if ok {
fmt.Println("This is true! Suprise!")
}
if ti != nil {
fmt.Println("This is true! Suprise!!")
}
if !reflect.ValueOf(ti).IsNil() { // the only right way to check underlying value of ti
fmt.Println("Never happen")
}
t, ok2 := res.(*MatchTeam)
if ok2 {
fmt.Println("This is true! Suprise!!!")
}
if t != nil {
fmt.Println("Never happen")
}
}
}