How in Go lang function returning multiple value can be passed to other function with a variadic parameter

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

How in Go lang function returning multiple value can be passed to other function with a variadic parameter

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go编程,

假设有以下代码,在函数fmt.Printf()中传递了两个不同的值:

package main

import "fmt"

func main() {
   var a int = 150
   var b int = 130
   var ret1, ret2 int
   ret1, ret2 = max(a, b)
   fmt.Printf("Max val: %d Min val: %d\n", ret1, ret2)
}

func max(num1, num2 int) (int, int) {
   if num1 > num2 {
      return num1, num2
   } else {
      return num2, num1
   }
}

fmt.Printf("Max val: %d Min val: %d\n", ret1, ret2)中,是否有办法直接传递max(a, b)fmt.Printf(),像这样:

fmt.Printf("Max val: %d Min val: %d\n", max(a, b))

我尝试过,但是出现了错误:

multiple-value max() in single-value context

看起来,返回两个不同的值并不是一个元组,我的概念对于返回值是错误的,或者我做错了什么。

是否有一种方法可以直接将具有多个返回值的函数传递给需要变量数量的值的函数

谢谢。

英文:

I am new to go programming,

Assume this code, where in function fmt.Printf() passing two different values

package main

import "fmt"

func main() {
   var a int = 150
   var b int = 130
   var ret1, ret2 int
   ret1, ret2 = max(a, b)
   fmt.Printf( "Max val: %d Min val: %d\n", ret1, ret2 )
}

func max(num1, num2 int) (int,int) {
   if (num1 > num2) {
      return num1, num2
   } else {
      return num2, num1
   }
}

Instead of passing ret1, ret2 in fmt.Printf( "Max val: %d Min val: %d\n", ret1, ret2 ) is there any way, I can directly pass max(a, b) in fmt.Printf() like

fmt.Printf( "Max val: %d Min val: %d\n", max(a, b))

I had tried it, it is giving error

> multiple-value max() in single-value context

As it seems, returning two different values not a tuple, is my concept for this returning is wrong or I am doing it wrong.

Is there any method to pass function with multiple return directly to function require variable number of values

TIA

答案1

得分: 4

根据规范

作为一个特例,如果函数或方法g的返回值的数量与另一个函数或方法f的参数数量相等,并且每个返回值都可以分配给f的参数,那么调用f(g(g的参数))将按顺序将g的返回值绑定到f的参数上后调用f。调用f除了调用g之外不能包含其他参数,并且g必须至少有一个返回值。如果f有一个最终的...参数,它将被分配给在分配了常规参数之后剩余的g的返回值。

你想要进行的调用的问题在于Printf的一个参数不是max的返回值。这个调用是有效的:

fmt.Println(max(a, b))

https://play.golang.org/p/ZZayVkwZJi

英文:

From the specification:

> As a special case, if the return values 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. The call of f must contain no parameters other than the call of g, and g must have at least one return value. If f has a final ... parameter, it is assigned the return values of g that remain after assignment of regular parameters.

The problem with the call that you want to make is that one of the arguments to Printf is not a return value of max. This call does work work:

fmt.Println(max(a, b))

https://play.golang.org/p/ZZayVkwZJi

答案2

得分: 1

你可以使用带有可变参数的内联辅助函数来实现类似的功能。

package main

import "fmt"

func main() {
   var a int = 150
   var b int = 130
   func(i ...int){fmt.Printf("最大值: %d 最小值: %d\n", i[0], i[1])}(max(a,b))
}

func max(num1, num2 int) (int,int) {
   if (num1 > num2) {
      return num1, num2
   } else {
      return num2, num1
   }
}

希望对你有所帮助。

请访问 https://play.golang.org/p/tB-q8hjNdu 查看它的运行效果。

英文:

You can do something similar with an inline helper function with a variadic parameter.

package main

import "fmt"

func main() {
   var a int = 150
   var b int = 130
   func(i ...int){fmt.Printf( "Max val: %d Min val: %d\n", i[0], i[1] )}(max(a,b))
}

func max(num1, num2 int) (int,int) {
   if (num1 > num2) {
      return num1, num2
   } else {
      return num2, num1
   }
}

I hope it helps.

Look at https://play.golang.org/p/tB-q8hjNdu to see it in action.

huangapple
  • 本文由 发表于 2016年12月10日 21:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/41076250.html
匿名

发表评论

匿名网友

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

确定