Skip to content

maps with non-primitive keys do not stringify correctly #222

Closed
@nlacasse

Description

@nlacasse

If you have a map with a struct type as the key type, then the values are printed as <nil> when you print the entire map.

    type T struct {
        i int
    }

    s := T{
        i: 1,
    }

    m := map[T]string{
        s: "foo",
    }

    // This correctly prints: m[s] = foo
    fmt.Printf("m[s] = %v\n", m[s])

    // This INCORRECTLY prints: m = map[{1}:<nil>]
    fmt.Printf("m = %v\n", m)


Possibly related:

I've been tracking down a bug involving the initialization of maps with struct keys, but I haven't found a clean repro case yet.

It has something to do with a struct-keyed map object being defined in package-level var or init function. In these cases it seems that the definitions of the map values are not included early enough in the generated JS file.

var (
  Foo = someTypeConstructor()
  m = map[otherType]thirdType{
    otherType{}: Foo.bar(),
  }
)

This version is too simple, and works fine. In my more complicated example, the compilation succeeds, but javascript complains that "Foo is undefined" when it tries to call Foo.bar(). The definition of Foo occurs 10 or so lines below in the generated JS.

I'm going to keep digging on this bug. Hopefully I can get a clean repro case soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions