根据分配给函数的变量数量,是否可能返回不同数量的值?

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

Is it possible to return a different number of values based on how many variables are assigned to the function?

问题

我有一个函数foo(),它返回一个int和一个bool,其中bool表示函数是否执行成功。如果调用者只想要int,他可以输入value, _ := foo()。有没有办法设置这样的方式,使得调用者只需输入value := foo(),它自动只返回int,就像range可以返回索引和值,或者只返回索引一样?

示例代码:

func foo(num int) (int, bool) {
	if num % 2 == 1 {
		//函数失败
		return 0, false
	}

	//函数成功
	return num + 5, true
}

func main() {
	value1, _ := foo(6) //可以工作

	value2 := foo(6) //赋值类型不匹配
}
英文:

I have a function, foo(), that returns an int and a bool, the bool being whether or not the function executed properly. If the caller only wants the int, he can type value, _ := foo(). Is there a way to set it so that the caller can just type value := foo() and it automatically only returns the int, like how range can return either the index and value, or just the index?

Example code:

func foo(num int) (int, bool) {
	if num % 2 == 1 {
		//function fails
		return 0, false
	}

	//function succeeds
	return num + 5, true
}

func main() {
	value1, _ := foo(6) //works

	value2 := foo(6) //assignment mismatch
}

答案1

得分: 2

Go编程语言的特殊功能包括映射(Maps)、类型断言(Type Assertions)、带有range子句的for语句以及通道接收具有可变数量的返回值。但对于常规函数类型,返回值的数量不能是可变的。

  • 映射(Maps)
m := make(map[string]int)

m["one"] = 1

// 返回值是给定键的映射值。
val := m["one"]

// 第一个返回值是给定键的映射值,第二个返回值指示键是否存在于映射中。
val, ok := m["one"]
  • 带有range子句的for语句
arr := []int{1, 2, 3, 4}

// 返回值是索引。
for idx := range arr {
 ...
}

// 第一个返回值是索引,第二个返回值是该索引处元素的副本。
for idx, val := range arr {
 ...
}
  • 类型断言(Type Assertions)

var i interface{}
i = 1

// 第一个返回值是底层值,第二个返回值是一个布尔值,报告断言是否成功。
num, ok := i.(int)

// 返回值是底层值。
num := i.(int)

  • 通道接收(Channel Receives)
c := make(chan int, 2)

c <- 12
c <- 21

// 返回值表示从通道接收到的值。
val := <-c

// 第一个返回值是从通道接收到的值。第二个返回值是一个布尔值,指示是否没有更多的值可接收并且通道是否已关闭。
val, ok := <-c

英文:

Special features of the Go programming language include maps, type assertions, for statement with a range clause and channel receives have variable number of return values. But the number of return values cannot be variable for a regular function type.

  • Maps
m := make(map[string]int)

m[&quot;one&quot;] = 1

// Return value is the value in the map for the given key.
val := m[&quot;one&quot;] 

// First return value is the value in the map for the given
// key and second return value indicates if the key was
// present in the map.
val,ok := m[&quot;one&quot;]
  • for statement with a range clause
arr := []int{1, 2, 3, 4}

// Return value is the index.
for idx := range arr {
 ...
}

// First return value is the index and second return 
// value is a copy of the element at that index.
for idx, val := range arr {
 ...
}
  • Type assertions

var i interface{}
i = 1

// First return value is the underlying value and second
// return value is a boolean value that reports whether the
// assertion succeeded or not.
num, ok := i.(int)

// Return value is the underlying value.
num := i.(int)

  • Channels receives
c := make(chan int, 2)

c &lt;- 12
c &lt;- 21

// Return value indicates the received value from the channel.
val := &lt;-c

// First return value is the received value from the
// channel. Second return value is a boolean which indicates
// if there are no more values to receive and the channel is
// closed or not.
val, ok := &lt;-c

huangapple
  • 本文由 发表于 2023年3月26日 01:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75843326.html
匿名

发表评论

匿名网友

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

确定