0

Assume there is a function that returns two variables.

func num(a,b int) (int,int) {
    return a+b, a-b
}

http://play.golang.org/p/bx05BugelV

And assume I have a function that only takes one int value.

package main

import "fmt"

func main() {
    fmt.Println("Hello, playground")
    _, a := num(1, 2)
    prn(a)

}

func num(a, b int) (int, int) {
    return a + b, a - b
}

func prn(a int) {
    fmt.Println(a)
}

http://play.golang.org/p/VhxF_lbVf4

Is there anyway I can only get the 2nd value (a-b) without having _,a:=num(1,2)?? Something like prn(num(1,2)[1]) <-- this won't work, but I'm wondering if there's a similar way

Thank you

1 Answer 1

3

Use a wrapper function. For example,

package main

import "fmt"

func main() {
    _, a := num(1, 2)
    prn(a)
    prn1(num(1, 2))

}

func num(a, b int) (int, int) {
    return a + b, a - b
}

func prn(a int) {
    fmt.Println(a)
}

func prn1(_, b int) {
    prn(b)
}

Output:

-1
-1
Sign up to request clarification or add additional context in comments.

1 Comment

I haven't thought about that. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.