不使用反射的两种解决方案:
将代码复制并粘贴到编辑器中:https: //play.golang.org/以查看实际情况。
1:在接口中添加“IsInterfaceNil()”函数。
2:使用A“类型开关”
下面的代码:
一一一一一一一一一一一一一一一一一一一一
示例 #1:IsInterfaceNil()
一一一一一一一一一一一一一一一一一一一一
//:Example #1:
//:I prefer this method because the
//:TakesInterface function does NOT need to know
//:about all the different implementations of
//:the interface.
package main;
import "fmt";
func main()(){
var OBJ_OK *MyStruct = &( MyStruct{} );
var NOT_OK *MyStruct = nil;
//:Will succeed:
TakesInterface( OBJ_OK );
//:Will fail:
TakesInterface( NOT_OK );
}
func TakesInterface( input_arg MyInterface ){
if( input_arg.IsInterfaceNil() ){
panic("[InputtedInterfaceIsNil]");
}
input_arg.DoThing();
}
type MyInterface interface{
DoThing()()
IsInterfaceNil()(bool)
}
type MyStruct struct{}
func(f *MyStruct)DoThing()(){
fmt.Println("[MyStruct.DoThing]");
}
func(f *MyStruct)IsInterfaceNil()(bool){
if(nil==f){ return true; }
return false;
}
一一一一一一一一一一一一一一一一一一一一
示例 #2:类型切换
一一一一一一一一一一一一一一一一一一一一
//:Example #2:
//:This will also work, but the function taking
//:the interface needs to know about all
//:implementations. This defeats a bit of the
//:decoupling from implementation that an
//:interface offers, but if you are just using
//:interfaces for polymorphism, it's probably
//:an okay way to go. (opinion)
package main;
import "fmt";
func main()(){
//:Will succeed:
var OBJ_OK *IMPLMENTS_INTERFACE_01 =
&( IMPLMENTS_INTERFACE_01{} );
TakesInterface( OBJ_OK );
//:Will fail:
var NOT_OK *IMPLMENTS_INTERFACE_01 = nil;
TakesInterface( NOT_OK );
}
func TakesInterface( hasDoThing MyInterface ){
//:THIS WILL NOT WORK:
if(nil==hasDoThing){
panic("[This_Error_Message_Will_Never_Happen]");
}
//:TYPE SWITCH TO THE RESCUE:
switch v := hasDoThing.(type){
case (*IMPLMENTS_INTERFACE_01):
if(nil==v){ panic("[Nil_PTR_01]"); }
case (*IMPLMENTS_INTERFACE_02):
if(nil==v){ panic("[Nil_PTR_02]"); }
case (*IMPLMENTS_INTERFACE_03):
if(nil==v){ panic("[Nil_PTR_03]"); }
default:
panic("[UnsupportedInterface]");
}
hasDoThing.DoThing();
}
type IMPLMENTS_INTERFACE_01 struct{};
type IMPLMENTS_INTERFACE_02 struct{};
type IMPLMENTS_INTERFACE_03 struct{};
func (f *IMPLMENTS_INTERFACE_01)DoThing()(){
fmt.Println( "DoingTheThing_01" );
}
func (f *IMPLMENTS_INTERFACE_02)DoThing()(){
fmt.Println( "DoingTheThing_02" );
}
func (f *IMPLMENTS_INTERFACE_03)DoThing()(){
fmt.Println( "DoingTheThing_03" );
}
type MyInterface interface{
DoThing()()
}
更新:
在我的代码库中实施后,我发现#2(类型切换)是最好的解决方案。特别是因为我不想在我正在使用的绑定库中编辑 glfw.Window 结构。这是我的用例的粘贴箱。为我的非标准编码风格道歉。https://pastebin.com/22SUDeGG