Extracting values from []interface{}

Hi All,

I am learning golang and need some help please.

i have a variable transformedEmail that looks like this when printed::

fmt.Printf("debug: %T, %v", transformedEmail, transformedEmail)

debug: []interface {}, [map[encoded_value:.YlGhCqAs@gmail.com] map[encoded_value:+61-442-291-954] map[encoded_value:9872-5721-5683-2126] map[encoded_value:830-726 79679125]]%

I want to loop through it and get the value of each “encoded_value”, how could I do it?


I have

I have tried to use transformedEmail[0] but I get following error:

invalid operation: cannot index transformedEmail (variable of type interface{})compilerNonIndexableOperand

Your transformedEmail must have been declared as type interface{}. The %T tell you its dynamic type is []interface{}. Use a type assertion to get the slice variable that you can index.

  transformedEmailSlice,ok := transformedEmail.([]interface{})
  if !ok {
    log.Fatalf("transformedEmail is not really a []interface{}")
  for i, element := range transformedEmailSlice {
    fmt.Printf("transformedEmail element %d: %T %v\n",i,element,element)

Thank you @mje , this worked!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.