Using map [string] int as a parameter of type map [interface {}] interface {}

I have a function:

func ReturnTuples(map_ map[interface{}]interface{}) [][]interface{} {

In what I'm trying to call like this:

m := make(map[string]int)
m["k1"] = 7
m["k2"] = 13
fmt.Println(ReturnTuples(m))

But I get

cannot use m (type map[string]int) as type map[interface {}]interface {} in argument to ReturnTuples

Does not it work with stringand intand realize interface{}?

I searched, and the best I could find was Convert the interface map [interface {}] {} to match the string , but it won’t answer why I can’t use it mas an argument.

I also believe that if there was only an argument to the function interface{}, that would also work, since it map[something][something]implements interface, right? What is the best way to do this, and why won't this work in my case?

+10
source share
2

, :

m := map[interface{}]interface{}

, "ReturnTuples".

: , , , interface{} interface{}

- , anything - , , for:

switch v := anything.(type) {
      case string:
            fmt.Println(v)
      case int32, int64:
            fmt.Println(v)
      case string:
            fmt.Println(v)
      case SomeCustomType:
            fmt.Println(v)
      default:
            fmt.Println("unknown")
}

"", @ymonad , .

,

PS: , ...

+7

assert .

func test(m interface{},key interface{}) bool { // Map is passed as reference
        ma := m.(map[interface{}]interface{})
        if _, ok := ma[key]; ok == false {
        ....
}
0

Source: https://habr.com/ru/post/1015209/


All Articles