How to convert interface{} to map

后端 未结 3 551
慢半拍i
慢半拍i 2021-01-31 08:43

I am trying to create a function that could accept following

*struct
[]*struct
map[string]*struct

Here struct could be any struct not just a sp

3条回答
  •  無奈伤痛
    2021-01-31 09:16

    Another way to convert an interface{} into a map with the package reflect is with MapRange.

    I quote:

    MapRange returns a range iterator for a map. It panics if v's Kind is not Map.

    Call Next to advance the iterator, and Key/Value to access each entry. Next returns false when the iterator is exhausted. MapRange follows the same iteration semantics as a range statement.

    Example:

    iter := reflect.ValueOf(m).MapRange()
    for iter.Next() {
        key := iter.Key().Interface()
        value := iter.Value().Interface()
        ...
    }
    

提交回复
热议问题