[go-nuts] instanceof

3,198 views
Skip to first unread message

Loa Lind

unread,
Apr 20, 2010, 4:57:49 AM4/20/10
to golang-nuts
I want to find if this object is an instance of this class - does Go
have a method which reminds of Java's instanceof?


--
Subscription settings: http://groups.google.com/group/golang-nuts/subscribe?hl=en

Jessta

unread,
Apr 20, 2010, 5:14:03 AM4/20/10
to Loa Lind, golang-nuts
On Tue, Apr 20, 2010 at 6:57 PM, Loa Lind <loa...@gmail.com> wrote:
> I want to find if this object is an instance of this class - does Go
> have a method which reminds of Java's instanceof?

You're probably looking for this:
http://golang.org/pkg/reflect/#Type.Typeof

But you can also use type switches:
http://golang.org/doc/go_spec.html#Switch_statements

- jessta
--
=====================
http://jessta.id.au

ceving

unread,
Apr 20, 2010, 5:16:06 AM4/20/10
to golang-nuts
On 20 Apr., 10:57, Loa Lind <loal...@gmail.com> wrote:
> I want to find if this object is an instance of this class - does Go
> have a method which reminds of Java's instanceof?
>

There are no classes. So the question can not be answered.

The only questions which can be answered are:
Is the var of a given type?
Is the var of a given interface?

Both question are quite boring, because of the static type system. You
already know the type because you have to declare the var with the
type. And the type can not change after that.

roger peppe

unread,
Apr 20, 2010, 5:16:56 AM4/20/10
to Loa Lind, golang-nuts
you can test whether an object (of an interface type)
satisfies another interface type, say X, by doing:

if _, ok := obj.(X); ok {
}

or you can test against several interfaces
at the same time by using a type switch:

switch obj.(type) {
case X:
....
case Y:
....
}

objects in go do not explicitly declare which interfaces
they implement.

see:
http://golang.org/doc/go_spec.html#Type_switches
http://golang.org/doc/go_spec.html#Type_assertions
http://golang.org/doc/go_spec.html#Interface_types

roger peppe

unread,
Apr 20, 2010, 5:20:44 AM4/20/10
to ceving, golang-nuts
On 20 April 2010 10:16, ceving <cev...@googlemail.com> wrote:
> The only questions which can be answered are:
> Is the var of a given type?
> Is the var of a given interface?
>
> Both question are quite boring, because of the static type system.

actually the second question isn't boring at all, because of the
dynamic type system :-)

Loa Lind

unread,
Apr 20, 2010, 5:26:33 AM4/20/10
to golang-nuts
Thanks! It worked perfect with a type switch :)

On 20 Apr., 11:14, Jessta <jes...@gmail.com> wrote:
> On Tue, Apr 20, 2010 at 6:57 PM, Loa Lind <loal...@gmail.com> wrote:
> > I want to find if this object is an instance of this class - does Go
> > have a method which reminds of Java's instanceof?
>
> You're probably looking for this:http://golang.org/pkg/reflect/#Type.Typeof
>
> But you can also use type switches:http://golang.org/doc/go_spec.html#Switch_statements
>
> - jessta
> --
> =====================http://jessta.id.au

ceving

unread,
Apr 20, 2010, 5:46:52 AM4/20/10
to golang-nuts
On 20 Apr., 11:20, roger peppe <rogpe...@gmail.com> wrote:
>
> actually the second question isn't boring at all, because of the
> dynamic type system :-)

I think most people would expect from a dynamic type system, that the
type checking will be done at run time.

As far as I understood this is not possible in Go:

(define a "moin")
(set! a 3.14)

If I try this in Go:

package main

import "fmt"

func main () {
a := "moin"
a = 3.14
fmt.Println (a);
}

I get:

cannot convert 3.14 to type string
cannot use 3.14 (type float) as type string in assignment

roger peppe

unread,
Apr 20, 2010, 5:54:20 AM4/20/10
to ceving, golang-nuts
On 20 April 2010 10:46, ceving <cev...@googlemail.com> wrote:
> On 20 Apr., 11:20, roger peppe <rogpe...@gmail.com> wrote:
>>
>> actually the second question isn't boring at all, because of the
>> dynamic type system :-)
>
> I think most people would expect from a dynamic type system, that the
> type checking will be done at run time.

some type checking is done at run time.

for instance:

package main
import "fmt"

func main() {
var a interface{} = "moin"
b := a.(float)
fmt.Println(b)
}

this compiles fine, but gives the error:
"panic: interface conversion: interface is string, not float"
at run time.

Ian Lance Taylor

unread,
Apr 20, 2010, 10:27:47 AM4/20/10
to ceving, golang-nuts
ceving <cev...@googlemail.com> writes:

> On 20 Apr., 11:20, roger peppe <rogpe...@gmail.com> wrote:
>>
>> actually the second question isn't boring at all, because of the
>> dynamic type system :-)
>
> I think most people would expect from a dynamic type system, that the
> type checking will be done at run time.

Go has both static and dynamic types. The only dynamic type is the
interface type.


> As far as I understood this is not possible in Go:
>
> (define a "moin")
> (set! a 3.14)
>
> If I try this in Go:
>
> package main
>
> import "fmt"
>
> func main () {
> a := "moin"
> a = 3.14
> fmt.Println (a);
> }
>
> I get:
>
> cannot convert 3.14 to type string
> cannot use 3.14 (type float) as type string in assignment


This program works:

package main

import "fmt"

func main() {
var a interface{} = "moin"
a = 3.14
fmt.Println(a)
}

Ian
Reply all
Reply to author
Forward
0 new messages