Monday, 15 February 2010

go - Golang use function that returns two variable -



go - Golang use function that returns two variable -

assume there function returns 2 variables.

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

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

and assume have function takes 1 int value.

package main import "fmt" func main() { fmt.println("hello, playground") _, := num(1, 2) prn(a) } func num(a, b int) (int, int) { homecoming + b, - b } func prn(a int) { fmt.println(a) }

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

is there anyway can 2nd value (a-b) without having _,a:=num(1,2)?? prn(num(1,2)[1]) <-- won't work, i'm wondering if there's similar way

thank you

use wrapper function. example,

package main import "fmt" func main() { _, := num(1, 2) prn(a) prn1(num(1, 2)) } func num(a, b int) (int, int) { homecoming + b, - b } func prn(a int) { fmt.println(a) } func prn1(_, b int) { prn(b) }

output:

-1 -1

go return

No comments:

Post a Comment