Println打印方括号,但接口不是切片。

huangapple go评论74阅读模式
英文:

Println prints square brackets but interface is not a slice

问题

我遇到了一个奇怪的接口问题,当我尝试打印一个值时,Println会添加方括号。我认为这是因为该接口包含切片,但我不确定应该如何迭代它们。

我很确定这是一个新手问题,但我花了很多时间寻找线索,却找不到任何线索。

此外,我无法在不使用goes的情况下重现它,所以这里是我实际使用的代码:

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\n", name)
    }
}

这将输出:

[One]
[]interface {}{"One"}
[Two]
[]interface {}{"Two"}
[Three]
[]interface {}{"Three"}

然而,如果我尝试像这样循环遍历"name":

for _, e := range name {
    fmt.Println(e)
}

我会得到"cannot range over name (type interface {})"的错误提示。

英文:

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\n", 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 {})"

答案1

得分: 1

使用类型断言。例如,

package main

import "fmt"

func main() {
    m := map[string]interface{}{}
    m["name"] = []interface{}{"One"}
    fmt.Println(m)

    name := m["name"]
    fmt.Println(name)
    fmt.Printf("%#v\n", name)

    for _, e := range name.([]interface{}) {
        fmt.Println(e)
    }
}

输出结果:

map[name:[One]]
[One]
[]interface {}{"One"}
One

Go编程语言规范

类型断言

对于一个接口类型的表达式 x 和一个类型 T,主表达式

x.(T)

断言 x 不是 nil,并且 x 中存储的值的类型是 T。x.(T) 的表示法称为类型断言。

英文:

Use a type assertion. For example,

package main

import "fmt"

func main() {
	m := map[string]interface{}{}
	m["name"] = []interface{}{"One"}
	fmt.Println(m)

	name := m["name"]
	fmt.Println(name)
	fmt.Printf("%#v\n", name)

	for _, e := range name.([]interface{}) {
		fmt.Println(e)
	}
}

Output:

map[name:[One]]
[One]
[]interface {}{"One"}
One

> The Go Programming Language Specification
>
> Type assertions
>
> For an expression x of interface type and a type T, the primary
> expression
>
> x.(T)
>
> asserts that x is not nil and that the value stored in x is of type T.
> The notation x.(T) is called a type assertion.

huangapple
  • 本文由 发表于 2014年10月4日 20:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/26192922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定