Type assertion between interface{} and []interface{}?

Written by
Link to Post

https://www.reddit.com/r/golang/comments/aepf02/type_assertion_between_interface_and_interface/

My main application has a map[string]map[string]interface{} in place of using a struct. It acts as a map of structs, with each field being its own dynamic interface, for example you could have Users["user1"]["Password"] = "password", or Users["user2"]["Admin"] = true.

However some “struct fields” I would like to use slices, for exampleUsers["user3"]["Todos"] = {"todo1", "todo2", "todo3"}. I’ve been stuck for a few days on appending an element to a slice in this way. The problem seems to be when trying to append one element at a time, particularly when trying to use type assertion between interface{} and []interface{}.

The expected output would be to have [todo1, todo2, todo3] when appending each time, but the actual output is [[todo1 todo2] todo3]. On the third iteration, even why using type assertion and variadic (...) operator, it treats the existing slice as if it is only a singular entity. Trying to iterate over the casted slice does nothing either.

https://play.golang.org/p/WmBfUGKQeGj

submitted by /u/dr3wy98
[link] [comments]

Article Tags:
· ·
Article Categories:
reddit

Leave a Reply