Type assertions go the other way.
You could write a CheckForInterface function with the reflection
package, I suppose. But I don't see the utility.
jimt has mentioned using type assertions, but I'd be interested
to know what you're trying to do with this check.
Chris
--
Chris "allusive" Dollin
> is an other option available ?
What are you trying to /do/?
In:
<<<<<<<
func main() {
var srv = NewServer()
defer srv.Exit("END")
fmt.Println("val = ", reflect.Typeof(srv))
fmt.Println("val = ", (*srv).(gomsgque.IServerSetup))
}
>>>>>>>
you are trying to print out a type representation for srv.
(You could use %T, which would be a lot nicer than
doing the reflect by hand.) But you /know/ what the
type of srv is: it's the type returned by NewServer, which
is *Server.
You can't type-test a non-interface type but you can
an interface type; so try
fmt.Println("val = ", interface{}(srv).(gomsgque.IServerSetup))
The `interface{}(s)` is a cast of srv to the type interface{},
which you can now apply the type-check to.
Different interfaces expose different behaviours, yes.
> JAVA
>
> JNIEXPORT void JNICALL NS(ContextCreate) (
> JNIEnv *env,
> jobject self
> )
> {
> ...
> // check for Server
> if ((*env)->IsInstanceOf(env, self, NS(Class_IServerSetup)) ==
> JNI_TRUE) {
> ErrorMqToJavaWithCheck (NS(ProcCreate)(context, self, NULL,
> NS(MID_IServerSetup_ServerSetup), NULL, &call));
> MqConfigSetServerSetup (context, NS(ProcCall), call, NS(ProcFree),
> NS(ProcCopy));
> }
That's not Java.
On 16 November 2010 18:48, gnu.bash <aott...@t-online.de> wrote:
There is no reason to need to know what interfaces a type satisfies at runtime.
You just make an interface for the behaviour your function requires
and the compiler will tell you if the types you're passing in satisfy
that interface.
- jessta
--
=====================
http://jessta.id.au
> Every language supporting interfaces support a function like
> "IsInstanceOf" in Java to check for the
> availability of this interface
And indeed you can do this in Go, as the message from me you quoted:
>> You can't type-test a non-interface type but you can
>> an interface type; so try
>>
>> fmt.Println("val = ", interface{}(srv).(gomsgque.IServerSetup))
>>
>> The `interface{}(s)` is a cast of srv to the type interface{},
>> which you can now apply the type-check to.
points out.
My mistake.
What I should have added was that to /test/ whether some interface
value V can be used as some type T, you need to use a type-testing
assignment (or declaration):
someT, isT := V.(T)
[or its big brother, the type switch)]
which will set isT to true if V can work as a T and someT to the T-shaped
value for V.
Using V.(T) as a single expression will deliver the T-value of V if
possible but otherwise panic.
and similiarly for the cleanup. Do not prepend an I to your interface names.