Skip to content

map with interface{} key does not accept concretely-typed key expressions #778

Closed
@gopherbot

Description

@gopherbot

by ehog.hedge:

What steps will reproduce the problem?
1. Attempt to compile

func main() {
    m := make(map[interface{}]interface{})
    m["key"] = 17
}

What is the expected output? 

Successful compilation.

What do you see instead?

invalid map index "key" - need type interface { }

What is your $GOOS?  $GOARCH?

amd64, linux

Which revision are you using?  (hg identify)

92e9a28d5886+ tip

Please provide any additional information below.

Introducing

  var s interface{} = "key"

and writing

  m[s] = 17

works. Defining

  type Any interface{}

and using

    m[Any("key")] = 17

produces

internal compiler error: typename ideal string

(Note: tried m[interface{}("key")] = 17; fails with a syntax
error, "unexpected interface", don't know whether this is a
bug or not, haven't looked yet.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions