当函数返回多个值时,如何在左侧显式地编写类型?

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

How to write the types explicitly on the left when the function returns multiple values?

问题

我可以这样做:

n, err := r.Read(b)

我也可以这样写:

var n, err = r.Read(b)

但是在左边,我想要显式地指定类型,像这样(因为这样不起作用):

var n, err int, error = r.Read(b)

我该如何做到这一点?

英文:

I can do

n,err:=r.Read(b)

Also I can write

var n,err=r.Read(b)

But on the left I want the types explicitly, like (because this does not work),

var n,err int,error=r.Read(b)

How do I do that?

答案1

得分: 1

唯一实现这一点的方法是在调用函数之前声明变量:

var n ini
var err error
n, err = r.Read(b)
英文:

The only way to accomplish this is having the variables declared before calling the function:

var n ini
var err error
n, err = r.Read(b)

huangapple
  • 本文由 发表于 2016年8月21日 03:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/39058054.html
匿名

发表评论

匿名网友

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

确定