I'm having a strange problem with interfaces, when I try to print a value Println adds square brackets. I believe this is because that interface contains slices, but I'm not sure how should iterate them.
I'm quite sure it's a newbie question but I spend alot time searching for a clue and can't find any.
Also I couldn't reproduce it without using goes, so here's the code that I actually used:
package main
import "fmt"
import "github.com/belogik/goes"
import "net/url"
func getConnection() (conn *goes.Connection) {
conn = goes.NewConnection("localhost", "9200")
return
}
func main() {
conn := getConnection()
var query = map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": map[string]interface{}{
"match_all": map[string]interface{}{},
},
},
},
"from": 0,
"size": 3,
"fields": []string{"name"},
}
extraArgs := make(url.Values)
searchResults, err := conn.Search(query, []string{"myindex"}, []string{"mytype"}, extraArgs)
if err != nil {
panic(err)
}
result := searchResults.Hits.Hits
for _,element := range result {
name := element.Fields["name"]
fmt.Println( name )
fmt.Printf( "%#v
", name )
}
}
This outputs:
[One]
[]interface {}{"One"}
[Two]
[]interface {}{"Two"}
[Three]
[]interface {}{"Three"}
However if I try to loop over the "name" like so:
for _, e := range name {
fmt.Println( e )
}
I'm getting "cannot range over name (type interface {})"