Re: Does Go support dynamic instance invocation?

130 views
Skip to first unread message

Dustin Sallings

unread,
Nov 5, 2012, 7:08:11 PM11/5/12
to golan...@googlegroups.com
Maxco <cobur...@gmail.com> writes:

> Something roughly equivalent to the Java snippet below:
>
>         HashMap hm = (HashMap)Class.forName( "java.util.HashMap"
> ).newInstance();
>         hm.put( "Hello", "World" );
>         System.out.println( "Hello " + hm.get( "Hello" ) );

That's barely legal java (HashMap is a generic type), and wouldn't
make sense as a translation to go. Do you have a particular problem
you're trying to solve?

In particular, if you know the type, you have an instance. Even in
the above code.

--
dustin

Devon H. O'Dell

unread,
Nov 5, 2012, 7:11:15 PM11/5/12
to Maxco, golang-nuts
It looks like you may be looking for http://golang.org/pkg/reflect/#New.

--dho

2012/11/5 Maxco <cobur...@gmail.com>:
> Something roughly equivalent to the Java snippet below:
>
> HashMap hm = (HashMap)Class.forName( "java.util.HashMap"
> ).newInstance();
> hm.put( "Hello", "World" );
> System.out.println( "Hello " + hm.get( "Hello" ) );
>
> --
>
>

Maxco

unread,
Nov 5, 2012, 7:35:21 PM11/5/12
to golan...@googlegroups.com, Maxco
Helpful thanks.
Reply all
Reply to author
Forward
0 new messages