FAQ
Been using angular with Go with much success but I need to know is there
any way to range over an arbitrary map?

I have my data map[string]*CustomData and want to send it to this function
without writing custom function for each data type.

func MapToJSObject(m map[string]interface) {

for a, b := range m {
                j, err := json.Encode(b)
                // serialise as JS object[a] = b
}

}

the golang.org people say that if i need to use reflection i'm doing
something wrong, please help

--
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

  • Alex Howard at Aug 29, 2014 at 1:41 pm
    Obviously I could just store the whole interface but i need to range over
    an arbitrary map
    On Friday, 29 August 2014 15:36:31 UTC+2, Alex Howard wrote:

    Been using angular with Go with much success but I need to know is there
    any way to range over an arbitrary map?

    I have my data map[string]*CustomData and want to send it to this function
    without writing custom function for each data type.

    func MapToJSObject(m map[string]interface) {

    for a, b := range m {
    j, err := json.Encode(b)
    // serialise as JS object[a] = b
    }

    }

    the golang.org people say that if i need to use reflection i'm doing
    something wrong, please help
    --
    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.
  • Lars Seipel at Aug 30, 2014 at 1:45 pm

    On Fri, Aug 29, 2014 at 06:41:33AM -0700, Alex Howard wrote:
    Obviously I could just store the whole interface but i need to range over
    an arbitrary map
    I don't think so. The values you want to take in your function must
    have *something* in common, otherwise you wouldn't be able to do
    anything meaningful with them. See if you can find that something and
    extract it into a type. Change your function to take a value of this
    type.

    Also look through the standard library or other high-quality Go code to
    see at what kind of solutions other people arrived.

    --
    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.
  • Rogerjd at Aug 30, 2014 at 3:13 pm
    https://groups.google.com/d/msg/golang-nuts/5PLkLjK3c6Y/M37bd5kB1SEJ
    Then, range over each value in the map,
    test that value in a type switch.
    Hope that helps.
    Roger

    On Saturday, August 30, 2014 9:46:21 AM UTC-4, Lars Seipel wrote:
    On Fri, Aug 29, 2014 at 06:41:33AM -0700, Alex Howard wrote:
    Obviously I could just store the whole interface but i need to range over
    an arbitrary map
    I don't think so. The values you want to take in your function must
    have *something* in common, otherwise you wouldn't be able to do
    anything meaningful with them. See if you can find that something and
    extract it into a type. Change your function to take a value of this
    type.

    Also look through the standard library or other high-quality Go code to
    see at what kind of solutions other people arrived.
    --
    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.
  • Alex Howard at Aug 30, 2014 at 3:42 pm
    Thanks for the link, only got so far as the
    http://godoc.org/github.com/mitchellh/mapstructure post so far

    seems to be a great package for unmarshalling map[string]interface{} into a
    struct with no fuss

    On 30 August 2014 17:13, rogerjd wrote:

    https://groups.google.com/d/msg/golang-nuts/5PLkLjK3c6Y/M37bd5kB1SEJ
    Then, range over each value in the map,
    test that value in a type switch.
    Hope that helps.
    Roger

    On Saturday, August 30, 2014 9:46:21 AM UTC-4, Lars Seipel wrote:
    On Fri, Aug 29, 2014 at 06:41:33AM -0700, Alex Howard wrote:
    Obviously I could just store the whole interface but i need to range over
    an arbitrary map
    I don't think so. The values you want to take in your function must
    have *something* in common, otherwise you wouldn't be able to do
    anything meaningful with them. See if you can find that something and
    extract it into a type. Change your function to take a value of this
    type.

    Also look through the standard library or other high-quality Go code to
    see at what kind of solutions other people arrived.
    --
    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.
  • Dario Castañé at Aug 30, 2014 at 3:52 pm
    You can also use Mergo, my library for merging and un/marshalling
    structs and maps: https://github.com/imdario/mergo

    Regards,

    Dario Castañé
    https://github.com/imdario/

    El 30/08/14 17:42, Alex Howard escribió:
    Thanks for the link, only got so far as the
    http://godoc.org/github.com/mitchellh/mapstructure post so far

    seems to be a great package for unmarshalling map[string]interface{} into a
    struct with no fuss

    On 30 August 2014 17:13, rogerjd wrote:

    https://groups.google.com/d/msg/golang-nuts/5PLkLjK3c6Y/M37bd5kB1SEJ
    Then, range over each value in the map,
    test that value in a type switch.
    Hope that helps.
    Roger

    On Saturday, August 30, 2014 9:46:21 AM UTC-4, Lars Seipel wrote:
    On Fri, Aug 29, 2014 at 06:41:33AM -0700, Alex Howard wrote:
    Obviously I could just store the whole interface but i need to range over
    an arbitrary map
    I don't think so. The values you want to take in your function must
    have *something* in common, otherwise you wouldn't be able to do
    anything meaningful with them. See if you can find that something and
    extract it into a type. Change your function to take a value of this
    type.

    Also look through the standard library or other high-quality Go code to
    see at what kind of solutions other people arrived.
    --
    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.
  • Alex Howard at Aug 30, 2014 at 4:32 pm
    I could optimise a lot of my Bitcoin API response handling code with that
    library thanks

    On 30 August 2014 17:52, Dario Castañé wrote:

    You can also use Mergo, my library for merging and un/marshalling
    structs and maps: https://github.com/imdario/mergo

    Regards,

    Dario Castañé
    https://github.com/imdario/

    El 30/08/14 17:42, Alex Howard escribió:
    Thanks for the link, only got so far as the
    http://godoc.org/github.com/mitchellh/mapstructure post so far

    seems to be a great package for unmarshalling map[string]interface{} into a
    struct with no fuss

    On 30 August 2014 17:13, rogerjd wrote:

    https://groups.google.com/d/msg/golang-nuts/5PLkLjK3c6Y/M37bd5kB1SEJ
    Then, range over each value in the map,
    test that value in a type switch.
    Hope that helps.
    Roger

    On Saturday, August 30, 2014 9:46:21 AM UTC-4, Lars Seipel wrote:
    On Fri, Aug 29, 2014 at 06:41:33AM -0700, Alex Howard wrote:
    Obviously I could just store the whole interface but i need to range over
    an arbitrary map
    I don't think so. The values you want to take in your function must
    have *something* in common, otherwise you wouldn't be able to do
    anything meaningful with them. See if you can find that something and
    extract it into a type. Change your function to take a value of this
    type.

    Also look through the standard library or other high-quality Go code to
    see at what kind of solutions other people arrived.
    --
    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.
  • Steve McCoy at Aug 29, 2014 at 2:30 pm
    I might be missing something, but why not json.Encode(m) ?

    On Friday, August 29, 2014 9:36:31 AM UTC-4, Alex Howard wrote:

    Been using angular with Go with much success but I need to know is there
    any way to range over an arbitrary map?

    I have my data map[string]*CustomData and want to send it to this function
    without writing custom function for each data type.

    func MapToJSObject(m map[string]interface) {

    for a, b := range m {
    j, err := json.Encode(b)
    // serialise as JS object[a] = b
    }

    }

    the golang.org people say that if i need to use reflection i'm doing
    something wrong, please help
    --
    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.
  • Alex Howard at Aug 29, 2014 at 3:15 pm
    I could do that for sure, but this was research into whether or not it is
    possible or not. I have concluded that it is impossible in go to do this
    type of reflection whether in a type switch or with reflect package:

    func SerializeEachRowSeparately(i interface{}) string {
    resolved_map, ok := i.(reflect.TypeOf(i))
    if !ok { return "FAILED" }
    for a, b := range resolved_map { // do something to the row }
    return ""
    }

    On Friday, 29 August 2014 16:30:06 UTC+2, Steve McCoy wrote:

    I might be missing something, but why not json.Encode(m) ?

    On Friday, August 29, 2014 9:36:31 AM UTC-4, Alex Howard wrote:

    Been using angular with Go with much success but I need to know is there
    any way to range over an arbitrary map?

    I have my data map[string]*CustomData and want to send it to this
    function without writing custom function for each data type.

    func MapToJSObject(m map[string]interface) {

    for a, b := range m {
    j, err := json.Encode(b)
    // serialise as JS object[a] = b
    }

    }

    the golang.org people say that if i need to use reflection i'm doing
    something wrong, please help
    --
    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.
  • Steve McCoy at Aug 29, 2014 at 4:12 pm
    It is possible. reflect.TypeOf(i) is not a compile-time construct, so you
    can't use it in a type assertion. You have to use runtime constructs all
    the way:

    func LoopThroughAnyMap(i interface{}) {
         m := reflect.ValueOf(i)
         for _, k := range m.MapKeys() {
             v := m.MapIndex(k)
             i := v.Interface()
             // do something with i
         }
    }

    On Friday, August 29, 2014 11:15:28 AM UTC-4, Alex Howard wrote:


    I could do that for sure, but this was research into whether or not it is
    possible or not. I have concluded that it is impossible in go to do this
    type of reflection whether in a type switch or with reflect package:

    func SerializeEachRowSeparately(i interface{}) string {
    resolved_map, ok := i.(reflect.TypeOf(i))
    if !ok { return "FAILED" }
    for a, b := range resolved_map { // do something to the row }
    return ""
    }

    On Friday, 29 August 2014 16:30:06 UTC+2, Steve McCoy wrote:

    I might be missing something, but why not json.Encode(m) ?

    On Friday, August 29, 2014 9:36:31 AM UTC-4, Alex Howard wrote:

    Been using angular with Go with much success but I need to know is there
    any way to range over an arbitrary map?

    I have my data map[string]*CustomData and want to send it to this
    function without writing custom function for each data type.

    func MapToJSObject(m map[string]interface) {

    for a, b := range m {
    j, err := json.Encode(b)
    // serialise as JS object[a] = b
    }

    }

    the golang.org people say that if i need to use reflection i'm doing
    something wrong, please help
    --
    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.
  • Alex Howard at Aug 29, 2014 at 9:20 pm
    Thanks a lot this was driving me insane
    On Friday, 29 August 2014 18:12:26 UTC+2, Steve McCoy wrote:

    It is possible. reflect.TypeOf(i) is not a compile-time construct, so you
    can't use it in a type assertion. You have to use runtime constructs all
    the way:

    func LoopThroughAnyMap(i interface{}) {
    m := reflect.ValueOf(i)
    for _, k := range m.MapKeys() {
    v := m.MapIndex(k)
    i := v.Interface()
    // do something with i
    }
    }

    On Friday, August 29, 2014 11:15:28 AM UTC-4, Alex Howard wrote:


    I could do that for sure, but this was research into whether or not it is
    possible or not. I have concluded that it is impossible in go to do this
    type of reflection whether in a type switch or with reflect package:

    func SerializeEachRowSeparately(i interface{}) string {
    resolved_map, ok := i.(reflect.TypeOf(i))
    if !ok { return "FAILED" }
    for a, b := range resolved_map { // do something to the row }
    return ""
    }

    On Friday, 29 August 2014 16:30:06 UTC+2, Steve McCoy wrote:

    I might be missing something, but why not json.Encode(m) ?

    On Friday, August 29, 2014 9:36:31 AM UTC-4, Alex Howard wrote:

    Been using angular with Go with much success but I need to know is
    there any way to range over an arbitrary map?

    I have my data map[string]*CustomData and want to send it to this
    function without writing custom function for each data type.

    func MapToJSObject(m map[string]interface) {

    for a, b := range m {
    j, err := json.Encode(b)
    // serialise as JS object[a] = b
    }

    }

    the golang.org people say that if i need to use reflection i'm doing
    something wrong, please help
    --
    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
postedAug 29, '14 at 1:36p
activeAug 30, '14 at 4:32p
posts11
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase