-3

I have the following anonymous struct:

func wrapHal(selfHref string) interface{} {
    return struct {
        _links struct {
            self struct {
                href string
            }
        }
    }{
        _links: {self: {href: selfHref}}, # this line
    }
}

However, in "this line, " I get the error missing type in composite literal

How to fix it? It is possible to initiate a anonymous nested struct in Go?

Rodrigo
  • 135
  • 4
  • 45
  • 107

1 Answers1

2

To initialize an anonymous struct, you have to declare the type. You declared the root anonymous struct, but you need to do again for each nested anonymous struct:

func wrapHal(selfHref string) interface{} {
    return struct {
        _links struct {
            self struct {
                href string
            }
        }
    }{
        _links: struct {
            self struct {
                href string
            }
        }{
            self: struct {
                href string
            }{
                href: "",
            },
        },
    }
}
Eduardo Thales
  • 401
  • 1
  • 8