Go方法失败:在单个值上下文中有多个值

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

Go method fails : multiple-value in a single value context

问题

我有以下的结构体:

  1. type OpList struct {
  2. Name xml.Name `xml:"Ser"`
  3. Servs []Ser `xml:"Ser"`
  4. }

我有一个方法:

  1. func GetInfo() (*OpList, error) {
  2. // 如果我在这里打印结果,它会被打印出来
  3. fmt.Println(OpList.Servs)
  4. return OpList, nil
  5. }

在方法内部访问列表是完全正常的。

但是当我调用这个方法并尝试访问它时,会出现错误消息:在单个值上下文中的多个值。

  1. bn := GetInfo()
  2. fmt.Printf(bn.Servs)

我在网络上也没有找到太多信息。我该如何访问从这样一个典型方法返回的值?

英文:

I have the following struct:

  1. type OpList struct {
  2. Name xml.Name `xml:"Ser"`
  3. Servs []Ser `xml:"Ser"`
  4. }

I have a method:

  1. func GetInfo() (*OpList, error){
  2. //If I print here the results gets printed
  3. fmt.Println(OpList.Servs)
  4. return OpList, nil
  5. }

Accessing the list works absolutely fine inside of the method

But when I call this method and try to access it fails with the message: multiple-value in a single value context

  1. bn:=GetInfo()
  2. fmt.Printf(bn.Servs)

I am actually not getting that much information in net as well. How do I access the value returned from a typical method like this?

答案1

得分: 6

尝试:

  1. bnerr := GetInfo()
  2. fmt.Printf(bn.Servs)
英文:

Try :

  1. bn, err := GetInfo()
  2. fmt.Printf(bn.Servs)

huangapple
  • 本文由 发表于 2014年8月26日 17:36:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/25502543.html
匿名

发表评论

匿名网友

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

确定