-1

I tried to search this and figure out exactly how it work but I'm having trouble finding an explanation.

If i have a variable data of type interface{} (data interface{})

What would eventData := data.(map[string]interface{}) be doing? I know interface can represent a number of things, but what is a high level overview of what his happening here?

Ayman Elmubark
  • 959
  • 1
  • 6
  • 11
  • 1
    It asserts that the concrete value stored in `data` is of type `map[string]interface{}`, and "extracts" this value and stores it in `eventData` (which will have a static type of `map[string]interface{}`. – icza Jul 28 '17 at 07:51
  • 1
    You can find more useful information about `interface{}` type here: https://stackoverflow.com/questions/23148812/go-whats-the-meaning-of-interface – Pavlo Strokov Jul 28 '17 at 07:53
  • Thanks for the explanations! – Ayman Elmubark Jul 28 '17 at 08:06

1 Answers1

1

It is a type assertion:

A type assertion provides access to an interface value's underlying concrete value.

t := i.(T)

https://tour.golang.org/methods/15

If the asserion does not hold it will trigger a panic. To test if the value is of specific type T you can use this:

t, ok := i.(T)

Ok is a boolean that is true if the assertion holds and false otherwise.

Arjan
  • 19,957
  • 2
  • 55
  • 48