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

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

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可以返回索引和值,或者只返回索引一样?

示例代码:

  1. func foo(num int) (int, bool) {
  2. if num % 2 == 1 {
  3. //函数失败
  4. return 0, false
  5. }
  6. //函数成功
  7. return num + 5, true
  8. }
  9. func main() {
  10. value1, _ := foo(6) //可以工作
  11. value2 := foo(6) //赋值类型不匹配
  12. }
英文:

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:

  1. func foo(num int) (int, bool) {
  2. if num % 2 == 1 {
  3. //function fails
  4. return 0, false
  5. }
  6. //function succeeds
  7. return num + 5, true
  8. }
  9. func main() {
  10. value1, _ := foo(6) //works
  11. value2 := foo(6) //assignment mismatch
  12. }

答案1

得分: 2

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

  • 映射(Maps)
  1. m := make(map[string]int)
  2. m["one"] = 1
  3. // 返回值是给定键的映射值。
  4. val := m["one"]
  5. // 第一个返回值是给定键的映射值,第二个返回值指示键是否存在于映射中。
  6. val, ok := m["one"]
  • 带有range子句的for语句
  1. arr := []int{1, 2, 3, 4}
  2. // 返回值是索引。
  3. for idx := range arr {
  4. ...
  5. }
  6. // 第一个返回值是索引,第二个返回值是该索引处元素的副本。
  7. for idx, val := range arr {
  8. ...
  9. }
  • 类型断言(Type Assertions)
  1. var i interface{}
  2. i = 1
  3. // 第一个返回值是底层值,第二个返回值是一个布尔值,报告断言是否成功。
  4. num, ok := i.(int)
  5. // 返回值是底层值。
  6. num := i.(int)
  • 通道接收(Channel Receives)
  1. c := make(chan int, 2)
  2. c <- 12
  3. c <- 21
  4. // 返回值表示从通道接收到的值。
  5. val := <-c
  6. // 第一个返回值是从通道接收到的值。第二个返回值是一个布尔值,指示是否没有更多的值可接收并且通道是否已关闭。
  7. 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
  1. m := make(map[string]int)
  2. m[&quot;one&quot;] = 1
  3. // Return value is the value in the map for the given key.
  4. val := m[&quot;one&quot;]
  5. // First return value is the value in the map for the given
  6. // key and second return value indicates if the key was
  7. // present in the map.
  8. val,ok := m[&quot;one&quot;]
  • for statement with a range clause
  1. arr := []int{1, 2, 3, 4}
  2. // Return value is the index.
  3. for idx := range arr {
  4. ...
  5. }
  6. // First return value is the index and second return
  7. // value is a copy of the element at that index.
  8. for idx, val := range arr {
  9. ...
  10. }
  • Type assertions
  1. var i interface{}
  2. i = 1
  3. // First return value is the underlying value and second
  4. // return value is a boolean value that reports whether the
  5. // assertion succeeded or not.
  6. num, ok := i.(int)
  7. // Return value is the underlying value.
  8. num := i.(int)
  • Channels receives
  1. c := make(chan int, 2)
  2. c &lt;- 12
  3. c &lt;- 21
  4. // Return value indicates the received value from the channel.
  5. val := &lt;-c
  6. // First return value is the received value from the
  7. // channel. Second return value is a boolean which indicates
  8. // if there are no more values to receive and the channel is
  9. // closed or not.
  10. 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:

确定