Golang多值函数组合

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

Golang multiple-value function composition

问题

我明白了,以下是翻译的内容:

在我在SO上没有找到答案的一个简短问题:当内部函数有多个返回值时,如何编写复合函数调用?

子问题:是否可以仅对多值函数的一个返回值进行类型转换,而不使用临时变量?

示例:http://play.golang.org/p/intnxkzSO1

  1. package main
  2. import "fmt"
  3. func multiReturn() (int, int) {
  4. return 0, 1
  5. }
  6. func noOp(a int) int {
  7. return a
  8. }
  9. func main() {
  10. // 参数过多
  11. fmt.Print(noOp(multiReturn()))
  12. // 多值在单值上下文中
  13. fmt.Print(string(multiReturn()))
  14. }
英文:

Short question to which I haven't found an answer on SO: how do I write composite function calls when an inner function has multiple return values?

Sub-question: can you cast just one of the returns from a multiple-value function without using a temp variable?

Example: http://play.golang.org/p/intnxkzSO1

  1. package main
  2. import "fmt"
  3. func multiReturn() (int, int) {
  4. return 0, 1
  5. }
  6. func noOp(a int) int {
  7. return a
  8. }
  9. func main() {
  10. // Too many arguments
  11. fmt.Print(noOp(multiReturn()))
  12. // multiple-value in single-value context
  13. fmt.Print(string(multiReturn()))
  14. }

答案1

得分: 2

你可以使外部函数的返回值与内部函数的返回值匹配。

  1. func multiReturn() (int, int) {
  2. return 10, 1
  3. }
  4. func noOp(a, _ int) int {
  5. return a
  6. }
  7. func main() {
  8. fmt.Printf("%v\n", noOp(multiReturn())) // 输出: 10
  9. fmt.Printf("%s", strconv.Itoa(noOp(multiReturn()))) // 输出: 10
  10. }

另外,需要注意的是,string(int) 不起作用,你需要使用 strconv 包。

在这里可以尝试运行代码:play

另一种选择是使用可变参数

  1. func noOp(a ...int) int {
  2. if len(a) > 0 {
  3. return a[0]
  4. }
  5. return 0
  6. }
英文:

You can have your outer function match the return values of the inner function

  1. func multiReturn() (int, int) {
  2. return 10, 1
  3. }
  4. func noOp(a, _ int) int {
  5. return a
  6. }
  7. func main() {
  8. fmt.Printf("%v\n", noOp(multiReturn())) // output: 10
  9. fmt.Printf("%s", strconv.Itoa(noOp(multiReturn()))) // output: 10
  10. }

On a side note string(int) will not work, you have to use the strconv package.

Go play

Another option would to use a variadic parameter.

  1. func noOp(a ...int) int {
  2. if len(a) > 0 {
  3. return a[0]
  4. }
  5. return 0
  6. }

答案2

得分: 0

例如,

  1. package main
  2. import "fmt"
  3. func singleReturn() int {
  4. s, _ := multiReturn()
  5. return s
  6. }
  7. func multiReturn() (int, int) {
  8. return 0, 1
  9. }
  10. func noOp(a int) int {
  11. return a
  12. }
  13. func main() {
  14. fmt.Print(noOp(singleReturn()))
  15. fmt.Print(string(singleReturn()))
  16. // 参数过多
  17. //fmt.Print(noOp(multiReturn()))
  18. // 多值在单值上下文中
  19. //fmt.Print(string(multiReturn()))
  20. }
英文:

For example,

  1. package main
  2. import "fmt"
  3. func singleReturn() int {
  4. s, _ := multiReturn()
  5. return s
  6. }
  7. func multiReturn() (int, int) {
  8. return 0, 1
  9. }
  10. func noOp(a int) int {
  11. return a
  12. }
  13. func main() {
  14. fmt.Print(noOp(singleReturn()))
  15. fmt.Print(string(singleReturn()))
  16. // Too many arguments
  17. //fmt.Print(noOp(multiReturn()))
  18. // multiple-value in single-value context
  19. //fmt.Print(string(multiReturn()))
  20. }

答案3

得分: -3

你也可以返回一个返回多个值的函数。

  1. func f() (int, int) {
  2. return 1, 2
  3. }
  4. func g() (int, int) {
  5. return f()
  6. }
英文:

you can also return a function that returns multiple values.

  1. func f() (int, int) {
  2. return 1, 2
  3. }
  4. func g() (int, int) {
  5. return f()
  6. }

huangapple
  • 本文由 发表于 2014年11月2日 03:31:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/26692327.html
匿名

发表评论

匿名网友

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

确定