Why you do not use the factory pattern?
(http://en.wikipedia.org/wiki/Factory_method_pattern)
It is simple to use and to extend.
You only need create() or new() and register() method.
The register() method need as parameters a string with the type name and
the creator function to create a object instance from that type.
create() or new() gets a string with the type name as parameter. It creates
with the registrated function
the wanted object.
Ok, you must register every supported type, but that work with own and
external types. And at the time
of implementing the factory, you do not need to know every type.
It is not so nice, like a type lookup per reflection, but it works (look as
an example in the Spring source code).
Dirk
Am Freitag, 14. September 2012 19:54:04 UTC+2 schrieb Robert Sandra:
Hi all,
I am trying to use reflect pkg to create my own "new()" function, such as
fund Newobj(x string) Value, inside my function, i will invoke Go's
build-in new() to create the obj. the parameter x stands for the type of
the new obj I'm going to create. The Newobj() function may looks like as
follows:
func Newobj(x string) Value{
new(*****);
}
for example, if I invoke Newobj("int"), I want this function to return a
value pointing to a "int" type. My question is I don't know how to transfer
the parameter "int" to the build-in new() function to let it create the
obj.
Thanks.
Robert
--
Dirk Jablonowski
(sorry for my bad english)
--