斯蒂芬大帝
迟到的答案,但这很好用package mainimport ( "fmt" "reflect")type SomeStruct1 struct{}type SomeStruct2 struct{}func IsInstanceOf(objectPtr, typePtr interface{}) bool { return reflect.TypeOf(objectPtr) == reflect.TypeOf(typePtr)}func main() { //sample variables someString := "Some String" someFloat := float32(2.4) someStruct1 := SomeStruct1{} someStruct2 := SomeStruct2{} someStruct1Ptr := &SomeStruct1{} // primitive string fmt.Println("string <-> *string \t\t", IsInstanceOf(someString, (*string)(nil))) //false fmt.Println("*string <-> *string \t\t", IsInstanceOf(&someString, (*string)(nil))) //true // primitive float32 fmt.Println("float32 <-> *float32 \t\t", IsInstanceOf(someFloat, (*float32)(nil))) //false fmt.Println("*float32 <-> *float32 \t\t", IsInstanceOf(&someFloat, (*float32)(nil))) //true // structure fmt.Println("SomeStruct1 <-> *SomeStruct1 \t", IsInstanceOf(someStruct1, (*SomeStruct1)(nil))) //false fmt.Println("*SomeStruct1 <-> *SomeStruct1 \t", IsInstanceOf(&someStruct1, (*SomeStruct1)(nil))) //true fmt.Println("*SomeStruct2 <-> *SomeStruct1 \t", IsInstanceOf(&someStruct2, (*SomeStruct1)(nil))) //false fmt.Println("*SomeStruct1 <-> *SomeStruct1 \t", IsInstanceOf(someStruct1Ptr, (*SomeStruct1)(nil))) //true}游乐场(在线运行): https: //play.golang.org/p/tcQqdzUGMlL