在一个具有多个返回值的函数调用中,如何只获取特定的值?

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

Get only a specific value out of a function call with multiple return values?

问题

给定一个返回多个值的函数swap,假设它是某个我无法修改的API的一部分:

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
}

是否可以使用swap函数并仅获取第一个返回值,丢弃第二个返回值?我尝试了以下代码:

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a)
}

错误信息:prog.go:10: b declared and not used [process exited with non-zero status]

而且以下代码也是不可能的:

a := swap("hello", "world")

错误信息:prog.go:10: multiple-value swap() in single-value context

当我不需要所有返回值时,如何处理返回多个值的函数?


代码基于"A tour of go - lesson 9"

英文:

Given swap, a function that returns multiple values, and supposing it's part of some API I can't modify:

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
}

Is it possible to use swap function and get only the first returned value, discarding the second one? I tried this:

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a)
}

ERROR: prog.go:10: b declared and not used [process exited with non-zero status]

And this is not possible too:

a := swap("hello", "world")

ERROR: prog.go:10: multiple-value swap() in single-value context

How to deal with functions that return multiple values when I don't need all the returned parts?


Code is based on "A tour of go - lesson 9"

答案1

得分: 12

使用空白标识符 _

a, _ := swap("hello", "world")

所有分配给空白标识符的内容都会被静默丢弃,不会引发警告。你还可以使用空白标识符来给结构体添加填充:

struct {
    a byte
    b byte
    c byte
    _ byte // 填充
}

空白标识符的另一个用途是在初始化全局变量时丢弃返回值:

var foo, _ = foo.NewFoo() // 忽略 NewFoo() 返回的错误

还有一种情况是当只需要范围中的一个值时:

for _, v := range mySlice { }
英文:

Use the blank identifier _:

a, _ := swap("hello", "world")

Everything assigned to the blank identifier is silently discarded without a warning being raised. You can also use the blank identifier to add padding to structures:

struct {
    a byte
    b byte
    c byte
    _ byte // padding
}

Another usage of the blank identifier is to discard return values when initializing global variables:

var foo, _ = foo.NewFoo() // ignore error returned by NewFoo()

And when only one value of a range is required:

for _, v := range mySlice { }

答案2

得分: 0

如果我们不想使用任何额外的变量(例如分配给映射键),我们可以使用立即调用的函数表达式(IIFE)。

searchParams[v] = func(i int64, err error) int { return int(i) }(strconv.ParseInt("100", 10, 0))

英文:

In case we don't want to use any extra variable (e.g. assigning to a map key), then we can use Immediately Invoked function expression (IIFE).

searchParams[v] = func(i int64, err error) int { return int(i) }(strconv.ParseInt("100", 10, 0))

huangapple
  • 本文由 发表于 2014年5月19日 18:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/23735432.html
匿名

发表评论

匿名网友

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

确定