FAQ
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

--

Search Discussions

  • Robert Sandra at Sep 14, 2012 at 6:04 pm
    To clarify my question, I want to implement the following Java actions in
    Go.

    public Object newInstance(String className, Object[] args) throws Exception {
    3 Class newoneClass = Class.forName(className);
    4
    5 Class[] argsClass = new Class[args.length];
    6
    7 for (int i = 0, j = args.length; i < j; i++) {
    8 argsClass[i] = args[i].getClass();
    9 }
    10
    11 Constructor cons = newoneClass.getConstructor(argsClass);
    12
    13 return cons.newInstance(args);
    14
    15 }

    My Newobj() function gets a string description of a Type as its parameter,
    then create and return an obj of this type.

    Thanks.
    On Friday, September 14, 2012 1:54:04 PM UTC-4, Robert Sandra wrote:

    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
    --
  • Kyle Lemons at Sep 14, 2012 at 6:09 pm
    No.

    You can't pass a type to a function, but you can pass a value, but if you
    do that you've already allocated it. new() is a builtin, not a function.
    You can use reflect.New() given a reflect.Type though, but afaik there is
    no way to express a reflect.Type literal.
    On Fri, Sep 14, 2012 at 10:54 AM, Robert Sandra wrote:

    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

    --

    --
  • Steven Rice at Sep 14, 2012 at 6:44 pm
    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Sebastien Binet at Sep 14, 2012 at 8:47 pm

    On Fri, Sep 14, 2012 at 8:36 PM, Steven Rice wrote:
    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    yep, or have each type register itself in a big map[string]func() interface{}...
    like:
    https://github.com/sbinet/go-croot/blob/master/pkg/croot/branch.go#L67
    https://github.com/sbinet/go-croot/blob/master/pkg/croot/object.go#L82

    -s

    --
  • Robert Sandra at Sep 14, 2012 at 9:23 pm
    Thanks Sebastien. I'll look into those code to see if I can get some sense
    from them. Thank you!
    On Friday, September 14, 2012 4:47:47 PM UTC-4, Sebastien Binet wrote:
    On Fri, Sep 14, 2012 at 8:36 PM, Steven Rice wrote:
    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    yep, or have each type register itself in a big map[string]func()
    interface{}...
    like:
    https://github.com/sbinet/go-croot/blob/master/pkg/croot/branch.go#L67
    https://github.com/sbinet/go-croot/blob/master/pkg/croot/object.go#L82

    -s
    --
  • Robert Sandra at Sep 14, 2012 at 9:14 pm
    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But an
    following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Steven Rice at Sep 14, 2012 at 9:44 pm
    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But an
    following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Robert Sandra at Sep 14, 2012 at 10:06 pm
    But the function missed a return statement, am I right? How can I define
    it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But an
    following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Steven Rice at Sep 14, 2012 at 10:19 pm
    Yeah, you can just create a return statement outside of the switch{} block
    that returns whatever value you'd like to use as indication that a case was
    not evaluated.
    I think you could even use "return 0" and then test if == 0. Like this:
    package main
    import "fmt"
    func Newobj(x string) (interface{}) {
    switch x {
    case "int": return new(int)
    case "foat32": return new(float32)
    }
    return 0
    }

    func main () {
    h1 := Newobj("inqt")
    if h1 != 0 {
    fmt.Printf("%v", h1)
    } else {
    fmt.Println("Error")
    }
    }
    On Friday, September 14, 2012 5:06:15 PM UTC-5, Robert Sandra wrote:

    But the function missed a return statement, am I right? How can I define
    it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But an
    following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Robert Sandra at Sep 15, 2012 at 2:34 am
    Thanks very much. The code works fine, but there is still a question. When
    I create the Newobj(), I don't know what user's own types there will be, so
    I can't just list all of the types in switch's cases..

    For example, in the users code, there is a struct:

    type Student struct {

    name string

    age int

    }

    While there is not any case related to "Student" in switch closure in
    Newobj(). So far what I can think is that in the switch closure, I add a
    default case as follows:

    default: return new(x)

    But x is not a type, so new(x) will failed.. So is there any way to solve
    this in Go?

    Thanks.



    On Friday, September 14, 2012 6:19:12 PM UTC-4, Steven Rice wrote:

    Yeah, you can just create a return statement outside of the switch{} block
    that returns whatever value you'd like to use as indication that a case was
    not evaluated.
    I think you could even use "return 0" and then test if == 0. Like this:
    package main
    import "fmt"
    func Newobj(x string) (interface{}) {
    switch x {
    case "int": return new(int)
    case "foat32": return new(float32)
    }
    return 0
    }

    func main () {
    h1 := Newobj("inqt")
    if h1 != 0 {
    fmt.Printf("%v", h1)
    } else {
    fmt.Println("Error")
    }
    }
    On Friday, September 14, 2012 5:06:15 PM UTC-5, Robert Sandra wrote:

    But the function missed a return statement, am I right? How can I define
    it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But an
    following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Steven Rice at Sep 15, 2012 at 3:03 am
    Well I have not familiarized myself with the reflect package so I am not
    sure if that may be able to help or not but you may consider reading the
    package documentation on it. Might I ask however why you want the argument
    to Newobj() be a string? Also might I ask the reason behind the function in
    the first place?
    On Friday, September 14, 2012 9:34:49 PM UTC-5, Robert Sandra wrote:

    Thanks very much. The code works fine, but there is still a question. When
    I create the Newobj(), I don't know what user's own types there will be, so
    I can't just list all of the types in switch's cases..

    For example, in the users code, there is a struct:

    type Student struct {

    name string

    age int

    }

    While there is not any case related to "Student" in switch closure in
    Newobj(). So far what I can think is that in the switch closure, I add a
    default case as follows:

    default: return new(x)

    But x is not a type, so new(x) will failed.. So is there any way to solve
    this in Go?

    Thanks.



    On Friday, September 14, 2012 6:19:12 PM UTC-4, Steven Rice wrote:

    Yeah, you can just create a return statement outside of the switch{}
    block that returns whatever value you'd like to use as indication that a
    case was not evaluated.
    I think you could even use "return 0" and then test if == 0. Like this:
    package main
    import "fmt"
    func Newobj(x string) (interface{}) {
    switch x {
    case "int": return new(int)
    case "foat32": return new(float32)
    }
    return 0
    }

    func main () {
    h1 := Newobj("inqt")
    if h1 != 0 {
    fmt.Printf("%v", h1)
    } else {
    fmt.Println("Error")
    }
    }
    On Friday, September 14, 2012 5:06:15 PM UTC-5, Robert Sandra wrote:

    But the function missed a return statement, am I right? How can I define
    it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But
    an following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Robert Sandra at Sep 15, 2012 at 3:09 am
    Actually the argument to Newobj() does not need to be a string. It can be
    anything if it can achieve my goal. What I want this function does is that
    it gets a description(or we can say a name) of a type, which can be Go's
    types or user's own types, and then create an obj of this type. Because the
    Newobj() is not a buildin, so it can't behave as the buildin new(), which
    can get a string "int" as its argument and create an int obj..
    On Friday, September 14, 2012 11:03:43 PM UTC-4, Steven Rice wrote:

    Well I have not familiarized myself with the reflect package so I am not
    sure if that may be able to help or not but you may consider reading the
    package documentation on it. Might I ask however why you want the argument
    to Newobj() be a string? Also might I ask the reason behind the function in
    the first place?
    On Friday, September 14, 2012 9:34:49 PM UTC-5, Robert Sandra wrote:

    Thanks very much. The code works fine, but there is still a question.
    When I create the Newobj(), I don't know what user's own types there will
    be, so I can't just list all of the types in switch's cases..

    For example, in the users code, there is a struct:

    type Student struct {

    name string

    age int

    }

    While there is not any case related to "Student" in switch closure in
    Newobj(). So far what I can think is that in the switch closure, I add a
    default case as follows:

    default: return new(x)

    But x is not a type, so new(x) will failed.. So is there any way to solve
    this in Go?

    Thanks.



    On Friday, September 14, 2012 6:19:12 PM UTC-4, Steven Rice wrote:

    Yeah, you can just create a return statement outside of the switch{}
    block that returns whatever value you'd like to use as indication that a
    case was not evaluated.
    I think you could even use "return 0" and then test if == 0. Like this:
    package main
    import "fmt"
    func Newobj(x string) (interface{}) {
    switch x {
    case "int": return new(int)
    case "foat32": return new(float32)
    }
    return 0
    }

    func main () {
    h1 := Newobj("inqt")
    if h1 != 0 {
    fmt.Printf("%v", h1)
    } else {
    fmt.Println("Error")
    }
    }
    On Friday, September 14, 2012 5:06:15 PM UTC-5, Robert Sandra wrote:

    But the function missed a return statement, am I right? How can I
    define it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But
    an following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Andy Balholm at Sep 15, 2012 at 3:24 am
    new() doesn't take a string as an argument. Otherwise it would be in quotes.

    --
  • Robert Sandra at Sep 15, 2012 at 3:32 am
    Yes, your are right.

    Suppose we have a user's struct Type Student, we can just use new(Student)
    to create a Student obj. What I want my Newobj() to do is that we can also
    use Newobj("Student") to create an Student obj. The argument of Newobj can
    be other things instead of a string.

    Thanks.
    On Friday, September 14, 2012 11:24:21 PM UTC-4, Andy Balholm wrote:

    new() doesn't take a string as an argument. Otherwise it would be in
    quotes.
    --
  • Alexei Sholik at Sep 15, 2012 at 6:58 am
    Robert, this is as far as you can go with reflect in Go -->
    http://play.golang.org/p/j-667shCCB

    As for creating a type from a string, I don't think it's possible in Go.
    It's not as dynamic as Java or Objective-C in this respect. Although Go
    does provide a way to enumerate the fields and methods of a type by their
    names. Take a look at the documentation for the reflect package.

    See related question on SO -->
    http://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st
    On 15 September 2012 06:32, Robert Sandra wrote:

    Yes, your are right.

    Suppose we have a user's struct Type Student, we can just use new(Student)
    to create a Student obj. What I want my Newobj() to do is that we can also
    use Newobj("Student") to create an Student obj. The argument of Newobj can
    be other things instead of a string.

    Thanks.

    On Friday, September 14, 2012 11:24:21 PM UTC-4, Andy Balholm wrote:

    new() doesn't take a string as an argument. Otherwise it would be in
    quotes.
    --



    --
    Best regards
    Alexei Sholik

    --
  • Alexei Sholik at Sep 15, 2012 at 6:15 pm
    You can pass a value of any type to the TypeOf method. See
    http://play.golang.org/p/ueh3iUt1uW

    The limitation is that you cannot use a type itself. Type literals are not
    first-class values in Go, but you can still obtain an instance of
    reflect.Type if you have a value of the desired type.
    On 15 September 2012 16:09, Robert Sandra wrote:

    Hi Alexei,

    Thanks very much for your reply.

    But in your example code, how can you deal with the Go's default types
    (e.g. its numeric types).

    For example, if we want to use new_obj(typ reflect.Type) to create an int
    obj, what argument can you use to invoke this function?

    Thank you.
    Robert
    On Sat, Sep 15, 2012 at 2:58 AM, Alexei Sholik wrote:

    Robert, this is as far as you can go with reflect in Go -->
    http://play.golang.org/p/j-667shCCB

    As for creating a type from a string, I don't think it's possible in Go.
    It's not as dynamic as Java or Objective-C in this respect. Although Go
    does provide a way to enumerate the fields and methods of a type by their
    names. Take a look at the documentation for the reflect package.

    See related question on SO -->
    http://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st
    On 15 September 2012 06:32, Robert Sandra wrote:

    Yes, your are right.

    Suppose we have a user's struct Type Student, we can just use
    new(Student) to create a Student obj. What I want my Newobj() to do is that
    we can also use Newobj("Student") to create an Student obj. The argument of
    Newobj can be other things instead of a string.

    Thanks.

    On Friday, September 14, 2012 11:24:21 PM UTC-4, Andy Balholm wrote:

    new() doesn't take a string as an argument. Otherwise it would be in
    quotes.
    --



    --
    Best regards
    Alexei Sholik

    --
    Best regards
    Alexei Sholik

    --
  • Robert Sandra at Sep 16, 2012 at 12:23 am
    Thanks! The code works very well for me. But I get a following problem. How
    can I read the obj's value after I created it?

    For example, as the following codes:

    func Newobj(typ reflect.Type) interface{} {

    i := reflect.New(typ).Interface()

    return i

    }

    j := concurrent.Newobj(reflect.TypeOf("string"))


    Next I want to create a read() function to read obj's value.

    func ReadObj(i interface()) interface{} {


    }

    If we know the obj's type is string in advance, we can use this expression
    in Readobj() to get its value:

    func ReadObj(i interface()) interface{} {

    return (*j.(*string))

    }

    But if we have no idea what type i is, I can only think that we may use :

    return (*j.(reflect.TypeOf(j)))


    But the Go's compiler posts an error saying that reflect.TypeOf(j) is not a
    type. I'am confused by this. Because the spec of reflect pkg is saying that
    TypeOf() returns a type..


    Thanks.



    On Saturday, September 15, 2012 2:15:05 PM UTC-4, alco wrote:

    You can pass a value of any type to the TypeOf method. See
    http://play.golang.org/p/ueh3iUt1uW

    The limitation is that you cannot use a type itself. Type literals are not
    first-class values in Go, but you can still obtain an instance of
    reflect.Type if you have a value of the desired type.

    On 15 September 2012 16:09, Robert Sandra <[email protected]<javascript:>
    wrote:
    Hi Alexei,

    Thanks very much for your reply.

    But in your example code, how can you deal with the Go's default types
    (e.g. its numeric types).

    For example, if we want to use new_obj(typ reflect.Type) to create an int
    obj, what argument can you use to invoke this function?

    Thank you.
    Robert

    On Sat, Sep 15, 2012 at 2:58 AM, Alexei Sholik <[email protected]<javascript:>
    wrote:
    Robert, this is as far as you can go with reflect in Go -->
    http://play.golang.org/p/j-667shCCB

    As for creating a type from a string, I don't think it's possible in Go.
    It's not as dynamic as Java or Objective-C in this respect. Although Go
    does provide a way to enumerate the fields and methods of a type by their
    names. Take a look at the documentation for the reflect package.

    See related question on SO -->
    http://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st

    On 15 September 2012 06:32, Robert Sandra <[email protected]<javascript:>
    wrote:
    Yes, your are right.

    Suppose we have a user's struct Type Student, we can just use
    new(Student) to create a Student obj. What I want my Newobj() to do is that
    we can also use Newobj("Student") to create an Student obj. The argument of
    Newobj can be other things instead of a string.

    Thanks.

    On Friday, September 14, 2012 11:24:21 PM UTC-4, Andy Balholm wrote:

    new() doesn't take a string as an argument. Otherwise it would be in
    quotes.
    --



    --
    Best regards
    Alexei Sholik

    --
    Best regards
    Alexei Sholik
    --
  • Robert Sandra at Sep 16, 2012 at 12:40 am
    I found the following works well when the obj j is a string

    fmt.Println("j value: ", reflect.ValueOf(j).Elem())


    But for other types, such as int and struct, it just prints j's address...

    On Saturday, September 15, 2012 8:23:08 PM UTC-4, Robert Sandra wrote:

    Thanks! The code works very well for me. But I get a following problem.
    How can I read the obj's value after I created it?

    For example, as the following codes:

    func Newobj(typ reflect.Type) interface{} {

    i := reflect.New(typ).Interface()

    return i

    }

    j := concurrent.Newobj(reflect.TypeOf("string"))


    Next I want to create a read() function to read obj's value.

    func ReadObj(i interface()) interface{} {


    }

    If we know the obj's type is string in advance, we can use this expression
    in Readobj() to get its value:

    func ReadObj(i interface()) interface{} {

    return (*j.(*string))

    }

    But if we have no idea what type i is, I can only think that we may use :

    return (*j.(reflect.TypeOf(j)))


    But the Go's compiler posts an error saying that reflect.TypeOf(j) is not
    a type. I'am confused by this. Because the spec of reflect pkg is saying
    that TypeOf() returns a type..


    Thanks.



    On Saturday, September 15, 2012 2:15:05 PM UTC-4, alco wrote:

    You can pass a value of any type to the TypeOf method. See
    http://play.golang.org/p/ueh3iUt1uW

    The limitation is that you cannot use a type itself. Type literals are
    not first-class values in Go, but you can still obtain an instance of
    reflect.Type if you have a value of the desired type.
    On 15 September 2012 16:09, Robert Sandra wrote:

    Hi Alexei,

    Thanks very much for your reply.

    But in your example code, how can you deal with the Go's default types
    (e.g. its numeric types).

    For example, if we want to use new_obj(typ reflect.Type) to create an
    int obj, what argument can you use to invoke this function?

    Thank you.
    Robert
    On Sat, Sep 15, 2012 at 2:58 AM, Alexei Sholik wrote:

    Robert, this is as far as you can go with reflect in Go -->
    http://play.golang.org/p/j-667shCCB

    As for creating a type from a string, I don't think it's possible in
    Go. It's not as dynamic as Java or Objective-C in this respect. Although Go
    does provide a way to enumerate the fields and methods of a type by their
    names. Take a look at the documentation for the reflect package.

    See related question on SO -->
    http://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st
    On 15 September 2012 06:32, Robert Sandra wrote:

    Yes, your are right.

    Suppose we have a user's struct Type Student, we can just use
    new(Student) to create a Student obj. What I want my Newobj() to do is that
    we can also use Newobj("Student") to create an Student obj. The argument of
    Newobj can be other things instead of a string.

    Thanks.

    On Friday, September 14, 2012 11:24:21 PM UTC-4, Andy Balholm wrote:

    new() doesn't take a string as an argument. Otherwise it would be in
    quotes.
    --



    --
    Best regards
    Alexei Sholik

    --
    Best regards
    Alexei Sholik
    --
  • Robert Sandra at Sep 16, 2012 at 12:44 am
    I found the following works well when the obj j is a string

    fmt.Println("j value: ", reflect.ValueOf(j).Elem())

    But it doesn't work well for other types, neither Go's numeric types or
    user's struct types..
    On Saturday, September 15, 2012 8:23:08 PM UTC-4, Robert Sandra wrote:

    Thanks! The code works very well for me. But I get a following problem.
    How can I read the obj's value after I created it?

    For example, as the following codes:

    func Newobj(typ reflect.Type) interface{} {

    i := reflect.New(typ).Interface()

    return i

    }

    j := concurrent.Newobj(reflect.TypeOf("string"))


    Next I want to create a read() function to read obj's value.

    func ReadObj(i interface()) interface{} {


    }

    If we know the obj's type is string in advance, we can use this expression
    in Readobj() to get its value:

    func ReadObj(i interface()) interface{} {

    return (*j.(*string))

    }

    But if we have no idea what type i is, I can only think that we may use :

    return (*j.(reflect.TypeOf(j)))


    But the Go's compiler posts an error saying that reflect.TypeOf(j) is not
    a type. I'am confused by this. Because the spec of reflect pkg is saying
    that TypeOf() returns a type..


    Thanks.



    On Saturday, September 15, 2012 2:15:05 PM UTC-4, alco wrote:

    You can pass a value of any type to the TypeOf method. See
    http://play.golang.org/p/ueh3iUt1uW

    The limitation is that you cannot use a type itself. Type literals are
    not first-class values in Go, but you can still obtain an instance of
    reflect.Type if you have a value of the desired type.
    On 15 September 2012 16:09, Robert Sandra wrote:

    Hi Alexei,

    Thanks very much for your reply.

    But in your example code, how can you deal with the Go's default types
    (e.g. its numeric types).

    For example, if we want to use new_obj(typ reflect.Type) to create an
    int obj, what argument can you use to invoke this function?

    Thank you.
    Robert
    On Sat, Sep 15, 2012 at 2:58 AM, Alexei Sholik wrote:

    Robert, this is as far as you can go with reflect in Go -->
    http://play.golang.org/p/j-667shCCB

    As for creating a type from a string, I don't think it's possible in
    Go. It's not as dynamic as Java or Objective-C in this respect. Although Go
    does provide a way to enumerate the fields and methods of a type by their
    names. Take a look at the documentation for the reflect package.

    See related question on SO -->
    http://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st
    On 15 September 2012 06:32, Robert Sandra wrote:

    Yes, your are right.

    Suppose we have a user's struct Type Student, we can just use
    new(Student) to create a Student obj. What I want my Newobj() to do is that
    we can also use Newobj("Student") to create an Student obj. The argument of
    Newobj can be other things instead of a string.

    Thanks.

    On Friday, September 14, 2012 11:24:21 PM UTC-4, Andy Balholm wrote:

    new() doesn't take a string as an argument. Otherwise it would be in
    quotes.
    --



    --
    Best regards
    Alexei Sholik

    --
    Best regards
    Alexei Sholik
    --
  • Steven Rice at Sep 16, 2012 at 12:51 am
    If you have a pointer to an object then you can just read that objects
    value by dereferencing the pointer. Like this:

    value = "Hello"
    pointerToValue = &value
    fmt.Printf("The value at address %v is %v", pointerToValue, *pointerToValue)
    On Saturday, September 15, 2012 7:23:08 PM UTC-5, Robert Sandra wrote:

    Thanks! The code works very well for me. But I get a following problem.
    How can I read the obj's value after I created it?

    For example, as the following codes:

    func Newobj(typ reflect.Type) interface{} {

    i := reflect.New(typ).Interface()

    return i

    }

    j := concurrent.Newobj(reflect.TypeOf("string"))


    Next I want to create a read() function to read obj's value.

    func ReadObj(i interface()) interface{} {


    }

    If we know the obj's type is string in advance, we can use this expression
    in Readobj() to get its value:

    func ReadObj(i interface()) interface{} {

    return (*j.(*string))

    }

    But if we have no idea what type i is, I can only think that we may use :

    return (*j.(reflect.TypeOf(j)))


    But the Go's compiler posts an error saying that reflect.TypeOf(j) is not
    a type. I'am confused by this. Because the spec of reflect pkg is saying
    that TypeOf() returns a type..


    Thanks.



    On Saturday, September 15, 2012 2:15:05 PM UTC-4, alco wrote:

    You can pass a value of any type to the TypeOf method. See
    http://play.golang.org/p/ueh3iUt1uW

    The limitation is that you cannot use a type itself. Type literals are
    not first-class values in Go, but you can still obtain an instance of
    reflect.Type if you have a value of the desired type.
    On 15 September 2012 16:09, Robert Sandra wrote:

    Hi Alexei,

    Thanks very much for your reply.

    But in your example code, how can you deal with the Go's default types
    (e.g. its numeric types).

    For example, if we want to use new_obj(typ reflect.Type) to create an
    int obj, what argument can you use to invoke this function?

    Thank you.
    Robert
    On Sat, Sep 15, 2012 at 2:58 AM, Alexei Sholik wrote:

    Robert, this is as far as you can go with reflect in Go -->
    http://play.golang.org/p/j-667shCCB

    As for creating a type from a string, I don't think it's possible in
    Go. It's not as dynamic as Java or Objective-C in this respect. Although Go
    does provide a way to enumerate the fields and methods of a type by their
    names. Take a look at the documentation for the reflect package.

    See related question on SO -->
    http://stackoverflow.com/questions/6390585/in-golang-is-is-possible-get-reflect-type-from-the-type-itself-from-name-as-st
    On 15 September 2012 06:32, Robert Sandra wrote:

    Yes, your are right.

    Suppose we have a user's struct Type Student, we can just use
    new(Student) to create a Student obj. What I want my Newobj() to do is that
    we can also use Newobj("Student") to create an Student obj. The argument of
    Newobj can be other things instead of a string.

    Thanks.

    On Friday, September 14, 2012 11:24:21 PM UTC-4, Andy Balholm wrote:

    new() doesn't take a string as an argument. Otherwise it would be in
    quotes.
    --



    --
    Best regards
    Alexei Sholik

    --
    Best regards
    Alexei Sholik
    --
  • Steven Rice at Sep 15, 2012 at 3:26 am
    Well I guess you could use the map[string]interface{} mentioned earlier and
    also create a register function that adds a type to the map
    On Friday, September 14, 2012 10:09:43 PM UTC-5, Robert Sandra wrote:

    Actually the argument to Newobj() does not need to be a string. It can be
    anything if it can achieve my goal. What I want this function does is that
    it gets a description(or we can say a name) of a type, which can be Go's
    types or user's own types, and then create an obj of this type. Because the
    Newobj() is not a buildin, so it can't behave as the buildin new(), which
    can get a string "int" as its argument and create an int obj..
    On Friday, September 14, 2012 11:03:43 PM UTC-4, Steven Rice wrote:

    Well I have not familiarized myself with the reflect package so I am not
    sure if that may be able to help or not but you may consider reading the
    package documentation on it. Might I ask however why you want the argument
    to Newobj() be a string? Also might I ask the reason behind the function in
    the first place?
    On Friday, September 14, 2012 9:34:49 PM UTC-5, Robert Sandra wrote:

    Thanks very much. The code works fine, but there is still a question.
    When I create the Newobj(), I don't know what user's own types there will
    be, so I can't just list all of the types in switch's cases..

    For example, in the users code, there is a struct:

    type Student struct {

    name string

    age int

    }

    While there is not any case related to "Student" in switch closure in
    Newobj(). So far what I can think is that in the switch closure, I add a
    default case as follows:

    default: return new(x)

    But x is not a type, so new(x) will failed.. So is there any way to
    solve this in Go?

    Thanks.



    On Friday, September 14, 2012 6:19:12 PM UTC-4, Steven Rice wrote:

    Yeah, you can just create a return statement outside of the switch{}
    block that returns whatever value you'd like to use as indication that a
    case was not evaluated.
    I think you could even use "return 0" and then test if == 0. Like this:
    package main
    import "fmt"
    func Newobj(x string) (interface{}) {
    switch x {
    case "int": return new(int)
    case "foat32": return new(float32)
    }
    return 0
    }

    func main () {
    h1 := Newobj("inqt")
    if h1 != 0 {
    fmt.Printf("%v", h1)
    } else {
    fmt.Println("Error")
    }
    }
    On Friday, September 14, 2012 5:06:15 PM UTC-5, Robert Sandra wrote:

    But the function missed a return statement, am I right? How can I
    define it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But
    an following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra
    wrote:
    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
    --
  • Peter at Sep 15, 2012 at 4:33 pm
    It would be more usual to return nil, rather than zero, since nil is the
    zero value of an interface.

    Note that if you were calling new("chan int"), it would return an interface
    wrapping a nil chan int, not nil itself, see
    http://golang.org/doc/go_faq.html#nil_error

    Otherwise you will need to make a type assertion before checking the return
    is equal to zero.
    On Friday, 14 September 2012 23:19:12 UTC+1, Steven Rice wrote:

    Yeah, you can just create a return statement outside of the switch{} block
    that returns whatever value you'd like to use as indication that a case was
    not evaluated.
    I think you could even use "return 0" and then test if == 0. Like this:
    package main
    import "fmt"
    func Newobj(x string) (interface{}) {
    switch x {
    case "int": return new(int)
    case "foat32": return new(float32)
    }
    return 0
    }

    func main () {
    h1 := Newobj("inqt")
    if h1 != 0 {
    fmt.Printf("%v", h1)
    } else {
    fmt.Println("Error")
    }
    }
    On Friday, September 14, 2012 5:06:15 PM UTC-5, Robert Sandra wrote:

    But the function missed a return statement, am I right? How can I define
    it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But an
    following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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
    --
  • Steven Rice at Sep 15, 2012 at 8:19 pm
    http://play.golang.org/p/_rbWi7-rdb You can test it here, no type assertion
    is necessary and no other situation will produce 0. I like to use numerical
    values for all my error codes. It makes the error handling system more
    consistent in it's struture.
    On Saturday, September 15, 2012 11:33:08 AM UTC-5, Peter wrote:

    It would be more usual to return nil, rather than zero, since nil is the
    zero value of an interface.

    Note that if you were calling new("chan int"), it would return an
    interface wrapping a nil chan int, not nil itself, see
    http://golang.org/doc/go_faq.html#nil_error

    Otherwise you will need to make a type assertion before checking the
    return is equal to zero.
    On Friday, 14 September 2012 23:19:12 UTC+1, Steven Rice wrote:

    Yeah, you can just create a return statement outside of the switch{}
    block that returns whatever value you'd like to use as indication that a
    case was not evaluated.
    I think you could even use "return 0" and then test if == 0. Like this:
    package main
    import "fmt"
    func Newobj(x string) (interface{}) {
    switch x {
    case "int": return new(int)
    case "foat32": return new(float32)
    }
    return 0
    }

    func main () {
    h1 := Newobj("inqt")
    if h1 != 0 {
    fmt.Printf("%v", h1)
    } else {
    fmt.Println("Error")
    }
    }
    On Friday, September 14, 2012 5:06:15 PM UTC-5, Robert Sandra wrote:

    But the function missed a return statement, am I right? How can I define
    it? Thank you very much.
    On Friday, September 14, 2012 5:44:20 PM UTC-4, Steven Rice wrote:

    Yeah, I typed Interface{} instead of interface{}
    On Friday, September 14, 2012 4:14:25 PM UTC-5, Robert Sandra wrote:

    Hi Steven,

    Thanks for helping. I just copied your code and tried to run it. But
    an following error came out for the first line of the Newobj() function:
    ./test.go:7: syntax error: unexpected { at end of statement

    I'm not sure if there is a typo in your code?

    Thanks.
    Robert

    On Friday, September 14, 2012 2:36:43 PM UTC-4, Steven Rice wrote:

    Why not something like this:
    #############################
    package main

    func Newobj(x string) Interface{} {
    switch x {
    case "int": return new(int)
    case "MyType" new(MyType)
    }
    }
    #############################
    On Friday, September 14, 2012 12:54:04 PM UTC-5, Robert Sandra wrote:

    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 at Sep 15, 2012 at 10:56 am
    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)



    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 14, '12 at 5:54p
activeSep 16, '12 at 12:51a
posts25
users8
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase