duan0403788996 2014-07-19 18:05
浏览 29
已采纳

出错-单值上下文中的多值fn()

I want to pass the results from a function fn() returning multiple values into a function wantx() that accepts multiple values. This seems to work if the number of values accepted by wantx() matches the number of return values. For example, fn() returns 2 values, and want2() accepts 2 values:

r:= want2( fn(5) )   // seems to work fine

However, if I want the return values of fn() to act as arguments 2 and 3 of want3(), then I get an error:

r:= want3( 1, fn(5) ) // error: multiple-value fn() in single-value context

How is want2() a multiple-value context while want3() is not ?

How do I get the call to want3() to work ?

Here is the full program:

package sandbox

import "testing"

func want3(fac int, i int, ok bool) int {
    if ok {
        return i * fac * 2
    }
    return i * fac * 3
}

func want2(i int, ok bool) int {
    if ok {
        return i * 2
    }
    return i * 3
}

func fn(i int) (int, bool) {
    return i, true
}

func TestCall(t *testing.T) {
    // error: multiple-value fn() in single-value context
    // r := want3(1, fn(5))

    r := want2( fn(5) )  // works fine

    if r != 10 {
        t.Errorf("Call!")
    }
}
  • 写回答

1条回答 默认 最新

  • doumowu7371 2014-07-19 18:17
    关注

    See here:

    As a special case, if the return parameters of a function or method g are equal in number and individually assignable to the parameters of another function or method f, then the call f(g(parameters_of_g)) will invoke f after binding the return values of g to the parameters of f in order.

    No other special cases for function calls are allowed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!