Agreed on the panic, and yours handles that better. (Although I hope it's obvious that this was just a toy example and not the real thing!) best,~ jf--John Feminella@jxxfhttp://jxf.me
On Sun, Feb 12, 2017 3:23 PM, Paul Borman bor...@google.com wrote: Your code can panic. Take a look at: https://play.golang.org/p/MrPtar9oEK traverse2 has the type assertion. Notice how the call to traverse2 works but the following call to traverse panics. -Paul On Sun, Feb 12, 2017 at 12:06 PM, John Feminella <j...@jxf.me> wrote: Thanks. I wound up doing this with a recursive traversal: https://play.golang.org/p/k9iy3sHOZ9 best,~ jf--John Feminella@jxxfhttp://jxf.me On Sun, Feb 12, 2017 3:05 PM, Paul Borman bor...@google.com wrote: Take a look at the reflect package, or if you know that the only internal maps will be of type map[string]interface{} you can use type assertion to determine if the element is a map or some other type. On Sun, Feb 12, 2017 at 7:53 AM, <j...@jxf.me> wrote: I would like to write a function which, given: * a `[]string` keys, and * a `map[string]interface{}` m, will return the value obtained by traversing `m` for each key in `keys`. For example, if `m` looks like: { "foo": 123, "bar": map[string]interface{}{ "one": 111, "two": "222222" }, } then I would like to be able to pass `["bar", "two"]` and receive `"222222"`. Similarly I would like to be able to pass ["foo"] and receive `123`. Does this make sense? Is this doable? -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.