On Thu, Aug 30, 2018, 18:57 Eric Raymond <e...@thyrsus.com> wrote: > I apologize if this seems like an elementary question, but web searches > and reading the Go documentation is not turning up an answer. > > I'm in the process of translating reposurgeon, an editor for > version-control histories, from Python into Go for improved performance, > .The central data structure is Repository, which consists mainly of a list > of events such as commits and tags. In Python, I use the language's late > binding and simply declare a list of containing multiple object types named > Commit, Tag, etc. There are also a number of lookup maps pointing at > objects in the main event list. Because objects in Pytron are passed by > reference everything is fairly straightforward. > > I'm trying to translate this to Go in a type-safe way. To do this, I need > to be able to write two declarations: "Slice of pointers to objects > satisfying the Event interface" >
[]*Event, but quite probably []Event is what is really needed. and "map of string keys to pointers to objects satisfying the Event > interface". > map[string]*Event, but once again, my bet is on map[string]Event. My attempts so far have yielded very cryptic error messages and no success. > It would probably help your case if you could provide some self-contained example reproducing the errors and post a link to the Go Playground. > Is it actually possible to restrict the main list's polymorphism in this > way and avoid casts? > I believe it is and that no conversions/type assertions are necessary to invoke the interface declared methods accros any instance satisfying Event in the list or map declared as suggested above. > > -- -j -- 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.