Patrick Reck
Patrick Reck

Reputation: 11374

Retrieving a value from an empty interface

I have an empty interface to which I have parsed some json data.

type Event interface {
}

As of now, the only value is name, and this is being set correctly. However, I cannot figure how to actually retrieve the value of this variable. How do I do that?

Upvotes: 2

Views: 920

Answers (1)

Mayank Patel
Mayank Patel

Reputation: 8546

If you did something this to unmarshel the json

var f interface{}
err := json.Unmarshal(b, &f)

You can use a type assertion to access f's underlying map[string]interface{}:

m := f.(map[string]interface{})

For more detail read this blog post.

Try It on Go Playground

Upvotes: 5

Related Questions