FAQ
Hi, I'm wondering how to do something like this? (using reflect package?)

package main

import (
"fmt"
)

type Material interface {
}

type Rock struct {
}

type Steel struct {
}

func main() {
var mat Material
mat = reflect.AllocByName("Rock")
fmt.Printf("mat = %v\n", mat)
}

Many thanks in advance
D

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Sebastien Binet at Jan 23, 2015 at 12:50 pm

    On Fri, Jan 23, 2015 at 1:33 PM, Dorival Pedroso wrote:
    Hi, I'm wondering how to do something like this? (using reflect package?)

    package main

    import (
    "fmt"
    )

    type Material interface {
    }

    type Rock struct {
    }

    type Steel struct {
    }

    func main() {
    var mat Material
    mat = reflect.AllocByName("Rock")
    fmt.Printf("mat = %v\n", mat)
    }

    Many thanks in advance
    the usual way is to register types during init:
    http://play.golang.org/p/4R4Auu1xgo

    hth,
    -s

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dorival Pedroso at Jan 23, 2015 at 11:10 pm
    thanks sebastian.
    i've been using this solution for a while and was just wondering if an
    alternative existed...
    cheers
    d
    On Friday, January 23, 2015 at 10:50:52 PM UTC+10, Sebastien Binet wrote:

    On Fri, Jan 23, 2015 at 1:33 PM, Dorival Pedroso
    <dorival...@gmail.com <javascript:>> wrote:
    Hi, I'm wondering how to do something like this? (using reflect package?)
    package main

    import (
    "fmt"
    )

    type Material interface {
    }

    type Rock struct {
    }

    type Steel struct {
    }

    func main() {
    var mat Material
    mat = reflect.AllocByName("Rock")
    fmt.Printf("mat = %v\n", mat)
    }

    Many thanks in advance
    the usual way is to register types during init:
    http://play.golang.org/p/4R4Auu1xgo

    hth,
    -s
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kostarev Ilya at Jan 23, 2015 at 12:52 pm
    Seems I misunderstand the question, but why just

    var mat, mat1, mat2, mat3 Material
    mat = Material(new(Rock))
    mat1 = Material(*new(Rock))
    mat2 = new(Rock)
    mat3 = *new(Rock)
    fmt.Printf("mat = %v\n", mat)
    fmt.Printf("mat1 = %v\n", mat1)
    fmt.Printf("mat2 = %v\n", mat2)
    fmt.Printf("mat3 = %v\n", mat3)

    doesn’t satisfy your needs?

    --
    Kostarev Ilya

    On 23 Jan 2015 at 15:33:08, Dorival Pedroso (dorival.pedroso@gmail.com) wrote:

    Hi, I'm wondering how to do something like this? (using reflect package?)

    package main

    import (
    "fmt"
    )

    type Material interface {
    }

    type Rock struct {
    }

    type Steel struct {
    }

    func main() {
    var mat Material
    mat = reflect.AllocByName("Rock")
    fmt.Printf("mat = %v\n", mat)
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dorival Pedroso at Jan 23, 2015 at 2:20 pm
    nope.
    i'd like to allocate a new variable based on it's name; .e.g a string that
    i've just read from file...

    something like this in python (but different):
    mat = getattr(sys.modules["filename.py"], "MyClass")()


    On Friday, January 23, 2015 at 10:52:35 PM UTC+10, Uvelichitel wrote:

    Seems I misunderstand the question, but why just

    var mat, mat1, mat2, mat3 Material
    mat = Material(new(Rock))
    mat1 = Material(*new(Rock))
    mat2 = new(Rock)
    mat3 = *new(Rock)
    fmt.Printf("mat = %v\n", mat)
    fmt.Printf("mat1 = %v\n", mat1)
    fmt.Printf("mat2 = %v\n", mat2)
    fmt.Printf("mat3 = %v\n", mat3)

    doesn’t satisfy your needs?

    --
    Kostarev Ilya

    On 23 Jan 2015 at 15:33:08, Dorival Pedroso (dorival...@gmail.com
    <javascript:>) wrote:

    Hi, I'm wondering how to do something like this? (using reflect package?)

    package main

    import (
    "fmt"
    )

    type Material interface {
    }

    type Rock struct {
    }

    type Steel struct {
    }

    func main() {
    var mat Material
    mat = reflect.AllocByName("Rock")
    fmt.Printf("mat = %v\n", mat)
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Jan 23, 2015 at 6:42 pm

    On Fri, Jan 23, 2015 at 6:20 AM, Dorival Pedroso wrote:

    i'd like to allocate a new variable based on it's name; .e.g a string that
    i've just read from file...

    something like this in python (but different):
    mat = getattr(sys.modules["filename.py"], "MyClass")()
    I don't know what that Python code does. But in Go you can't look up
    a type, or a variable, by name at run time.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dorival Pedroso at Jan 23, 2015 at 11:15 pm
    just for ref. i've been using something like this in python:

    # load element files
    type2class = {} # maps element type to its class object
    for p in params.values(): # for each set of parameters
         typ = p['type'] # element name/type, e.g 'Rock'
         mod = 'mod.' + etyp # element module to be imported
         __import__(mod) # load module by its name, e.g 'Rock'
         type2class[typ] = getattr(sys.modules[mod], typ) # store class object

    # usage
    elems = []
    for typ in ['Rock','Steel',...]:
         elems.append(type2class[typ](...))

    On Saturday, January 24, 2015 at 4:42:55 AM UTC+10, Ian Lance Taylor wrote:

    On Fri, Jan 23, 2015 at 6:20 AM, Dorival Pedroso
    <dorival...@gmail.com <javascript:>> wrote:
    i'd like to allocate a new variable based on it's name; .e.g a string that
    i've just read from file...

    something like this in python (but different):
    mat = getattr(sys.modules["filename.py"], "MyClass")()
    I don't know what that Python code does. But in Go you can't look up
    a type, or a variable, by name at run time.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kevin Malachowski at Jan 24, 2015 at 12:33 am
    If you're okay with listing the types you want to use (like you do in the
    Python) then you can get away with it in
    Go: http://play.golang.org/p/jqDyMgHiYJ

    The limitation is that in Go the linker strips any code and types that are
    statically known to never be used (a feature to prevent super bloated
    binaries): this is why you can't get a type by name in general. This code
    ensures the type is included in the binary and then uses reflect magic to
    just help allocate new memory for the new value.

    If you were okay with going down the road of a machine generated solution
    (via go generate <http://blog.golang.org/generate>, possibly) you could aim
    for something like this: http://play.golang.org/p/QHdBsfsOfw As long as
    you're still alright with manually listing the types you want then the
    generator program will be super straightforward. If you wanted the machine
    to automagically figure out what types implement a specific interface you
    still could do that, but I wouldn't know where to start. There are so many
    official and unofficial packages for consuming go source code, though, that
    I'd bet someone's probably already solved the "tell me all types that
    implement this interface" problem.

    On Friday, January 23, 2015 at 3:15:55 PM UTC-8, Dorival Pedroso wrote:

    just for ref. i've been using something like this in python:

    # load element files
    type2class = {} # maps element type to its class object
    for p in params.values(): # for each set of parameters
    typ = p['type'] # element name/type, e.g 'Rock'
    mod = 'mod.' + etyp # element module to be imported
    __import__(mod) # load module by its name, e.g 'Rock'
    type2class[typ] = getattr(sys.modules[mod], typ) # store class object

    # usage
    elems = []
    for typ in ['Rock','Steel',...]:
    elems.append(type2class[typ](...))

    On Saturday, January 24, 2015 at 4:42:55 AM UTC+10, Ian Lance Taylor wrote:

    On Fri, Jan 23, 2015 at 6:20 AM, Dorival Pedroso
    wrote:
    i'd like to allocate a new variable based on it's name; .e.g a string that
    i've just read from file...

    something like this in python (but different):
    mat = getattr(sys.modules["filename.py"], "MyClass")()
    I don't know what that Python code does. But in Go you can't look up
    a type, or a variable, by name at run time.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Carlos Castillo at Jan 23, 2015 at 7:55 pm
    You can't load code at runtime, so there is no benefit to finding a
    class/type by name, since all will be known at compile time. That means
    that you can build a map[string]interface{}, or
    map[string]func()interface{} that does what you want and never stray near
    reflect, unsafe, or dynamically loading code.

    Look at the database/sql package to see how drivers are loaded by name in a
    string, and at a database driver to see how one registers itself with
    database/sql in an init function.
    On Friday, January 23, 2015 at 6:20:09 AM UTC-8, Dorival Pedroso wrote:

    nope.
    i'd like to allocate a new variable based on it's name; .e.g a string that
    i've just read from file...

    something like this in python (but different):
    mat = getattr(sys.modules["filename.py"], "MyClass")()


    On Friday, January 23, 2015 at 10:52:35 PM UTC+10, Uvelichitel wrote:

    Seems I misunderstand the question, but why just

    var mat, mat1, mat2, mat3 Material
    mat = Material(new(Rock))
    mat1 = Material(*new(Rock))
    mat2 = new(Rock)
    mat3 = *new(Rock)
    fmt.Printf("mat = %v\n", mat)
    fmt.Printf("mat1 = %v\n", mat1)
    fmt.Printf("mat2 = %v\n", mat2)
    fmt.Printf("mat3 = %v\n", mat3)

    doesn’t satisfy your needs?

    --
    Kostarev Ilya

    On 23 Jan 2015 at 15:33:08, Dorival Pedroso (dorival...@gmail.com) wrote:

    Hi, I'm wondering how to do something like this? (using reflect package?)

    package main

    import (
    "fmt"
    )

    type Material interface {
    }

    type Rock struct {
    }

    type Steel struct {
    }

    func main() {
    var mat Material
    mat = reflect.AllocByName("Rock")
    fmt.Printf("mat = %v\n", mat)
    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 23, '15 at 12:33p
activeJan 24, '15 at 12:33a
posts9
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase