On 2 November 2016 at 14:44, Bill Warner <wwar...@gmail.com> wrote: > Same thing, but keys can be ints. https://play.golang.org/p/2YssTy0dY5
I considered that, but I think that in practice []string is easier to deal with than []interface{} for a set of indexes, and you'll need to deal with an error in both cases. In general I'd try to avoid []interface{} unless the elements really can be anything. Your code there will panic if there's a slice where you're expecting an object. I guess it depends how you're intending to use this thing. I was imagining something like this: https://play.golang.org/p/gReMKhNe2q or you could decide to continue with the strict division between integer and string indexes and still avoid use of []interface{}: https://play.golang.org/p/_kPcsf39Mo > > On 11/2/16 4:33 AM, roger peppe wrote: >> >> I'd suggest something like this: >> >> https://play.golang.org/p/w1Vxg6b2Ey >> >> Not too much repeated code there, and no need for the reflect package >> either. >> >> cheers, >> rog. >> >> On 1 November 2016 at 17:13, <wwar...@gmail.com> wrote: >>> >>> Basically, the idea is: given a json structure, and a list of strings or >>> integers, treat the list as a path into the structure and return the >>> value >>> at that point. There are some things this doesn't do yet, among them: >>> handle >>> float values, handle slice indexes that are out of range, handle paths >>> that >>> are longer than the struct is deep, etc. But what i've shared runs >>> against >>> the sample json. >>> >>> >>> On Tuesday, November 1, 2016 at 1:04:21 PM UTC-4, wwa...@gmail.com wrote: >>>> >>>> https://play.golang.org/p/LhOTUuTVqA >>>> >>>> On Tuesday, November 1, 2016 at 12:50:09 PM UTC-4, Bill Warner wrote: >>>>> >>>>> Yes it's just a fragment. Let me clean it up a bit, then I'll share a >>>>> playground link. >>>>> >>>>> On 11/1/16 12:47 PM, Volker Dobler wrote: >>>>> >>>>> Am Dienstag, 1. November 2016 02:07:49 UTC+1 schrieb wwa...@gmail.com: >>>>>> >>>>>> Hello all, >>>>>> >>>>>> I'm new to Go, and I have a question about identifying types as >>>>>> they're >>>>>> encountered in traversing a map[string]interface{}. >>>>>> >>>>>> I've ended up with a big sieve of type assertions something like this: >>>>>> >>>>>> if mt.Mi, ok = m.(map[string]int); ok { >>>>>> nval, ok = mt.Mi[mk] >>>>>> } else if mt.MI, ok = m.(map[string]interface{}); ok { >>>>>> nval, ok = mt.MI[mk] >>>>>> } else if mt.Mai, ok = m.(map[string][]int); ok { >>>>>> nval, ok = mt.Mai[mk] >>>>>> } else if mt.Mas, ok = m.(map[string][]string); ok { >>>>>> nval, ok = mt.Mas[mk] >>>>>> } else if mt.Mmm, ok = m.(map[string]map[string]interface{}); >>>>>> ok >>>>>> { >>>>>> nval, ok = mt.Mmm[mk] >>>>>> >>>>>> mt here is a struct that performs no work; it just associates a type >>>>>> to >>>>>> a name, so that the run-time can see the types of the left and the >>>>>> right >>>>>> sides of the assignment and determine if an assignment is possible. I >>>>>> really >>>>>> hate looking at that statement, but all my attempts at using >>>>>> reflection have >>>>>> failed as the compiler can't allocate with all the possible types that >>>>>> could >>>>>> be returned, even though in my application I only want to allocate for >>>>>> these >>>>>> five types. So that's my question: Can I DRY this up? >>>>> >>>>> >>>>> The code you showed is basically a noop: If this is going to compile >>>>> than nval must be of type interface {} and you could replace all this >>>>> with a simple >>>>> nval = m >>>>> You either did not show the last else-block or something is strange >>>>> here. >>>>> >>>>> V. >>>>> >>>>> >>>>> >>> -- >>> 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.