'Unable to convert interface type to custom type in Go? [duplicate]
I am trying to type convert an interface to a struct
Why?
This is because I don't know before hand what the type of payload i will get, so the default type is interface{}
BUT, using a switch case, I need to convert the interface type to parse payload using a specific function:
/*********************************************
TYPES:
EventPayload is of type -> interface{}
NewUserPayload is of type -> struct {
Username string `json:"username"`
UserId string `json:"userId"`
}
*********************************************/
switch payload.EventName {
case events.NEW_USER:
c.newUserHandler(payload.EventPayload.(core.NewUserPayload)) // 👈 error
case events.DIRECT_MESSAGE:
c.directMessageHandler(payload.EventPayload.(core.DirectMessagePayload))
}
Error message:
interface conversion: interface {} is map[string]interface {}, not core.NewUserPayload
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
