Reputation: 733
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
Upvotes: 0
Views: 239
Reputation: 166539
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
Upvotes: 3