Here's the sample code (from tour.golang.org),
package main
import "fmt"
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return
}
func main() {
x, _ := split(17)
fmt.Println(x)
}
I would like to know if it is possible to shorten the main function to just a single line. Logically there should be a way (might not look very elegant in this case though) to access only first result parameter returned from function and print it.
fmt.Println
is a variadic function, so you can just pass the split function as a parameter:
func main() {
fmt.Println(split(17))
}
see it working in full here: http://play.golang.org/p/c1zkFVMe11