Golang:在返回多个参数的函数中,使用其中一个值作为条件。

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

Golang: Use one value in conditional from function returning multiple arguments

问题

假设在Go语言中,我们有一个返回两个参数的函数:

func squareAndCube(side int) (square int, cube int) {
    square = side * side
    cube = square * side
    return
}

然后你想在条件语句中使用该函数的第一个(或第二个)返回值:

square, _ := squareAndCube(n)
if square > m {
    ...
}

然而,如果我们不需要将square的值用在其他地方,我们能否将这两行代码合并成一行呢?例如:

if squareAndCube(n).First() > m {
    ...
}

请注意,Go语言中没有直接获取函数返回值的方法,所以无法使用.First()这样的语法来获取第一个返回值。你需要使用两行代码来获取并忽略第二个返回值。

英文:

Suppose in Go we have a function returning two arguments

func squareAndCube(int side) (square int, cube int) {
    square = side * side
    cube = square * side
    return
}

Then you would like to use the first (second) value of this function in the conditional:

square, _ := squareAndCube(n)
if square > m {
    ...
}

However, can we do first two lines in one line if we do not need the value square to use anywhere else? E.g.

 if squareAndCube(n).First() > m {
     ...
 }

答案1

得分: 51

你不能选择多个返回值中的一个,但你可以写出类似这样的代码:

if square, _ := squareAndCube(n); square > m {
    // ...
}

square 变量只在 if 语句的作用域内有效。这些“简单语句”可以在 if 语句switch 语句 和其他结构(如 for 循环)中使用。

另请参阅 effective go 文章中的 if 语句

英文:

You cannot pick one of the multiple returned values but you can write something like

if square, _ := squareAndCube(n); square > m {
    // ...
}

The square variable will only be valid in the if scope. These "simple statements" can be used in if statements, switch statements and other constructs such as for loops.

See also the effective go article on if statements.

答案2

得分: 9

Vladimir Vivien的博客文章中找到了一个解决该问题的巧妙方法。解决方案是创建一个函数,利用编译器的自动转换将形式为"x...interface{}"的可变参数转换为标准的[]interface{}。

func mu(a ...interface{}) []interface{} {
    return a
}

现在你可以将任何具有多个返回值的函数包装在`mu`并在返回的切片后面进行类型断言

```go
package main

import(
    "fmt"
)

func mu(a ...interface{}) []interface{} {
    return a
}

func myFunc(a,b string) (string, string){
    return b, a
}

func main(){
    fmt.Println(mu(myFunc("Hello", "World"))[1].(string))
}

// 输出: Hello

编辑:参见Matt Mc评论

英文:

Found this blog post by Vladimir Vivien that has a <s>nice</s> workaround for the problem. The solution is to create a function that "...exploits the automatic conversion from the compiler to convert a vararg parameters in the form of "x...interface{}" to a standard []interface{}."

func mu(a ...interface{}) []interface{} {
    return a
}

Now you can wrap any function with multiple returned values in mu and index the returned slice followed by a type assertion

package main

import(
	&quot;fmt&quot;
)

func mu(a ...interface{}) []interface{} {
    return a
}

func myFunc(a,b string) (string, string){
	return b, a
}

func main(){
	fmt.Println(mu(myFunc(&quot;Hello&quot;, &quot;World&quot;))[1].(string))
}

// output: Hello

EDIT: See comment by Matt Mc

huangapple
  • 本文由 发表于 2014年7月23日 21:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/24911993.html
匿名

发表评论

匿名网友

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

确定