Home >Backend Development >Golang >How to Pass Multiple Return Values to a Variadic Function in Go?
Passing Multiple Return Values to a Variadic Function
Problem:
You have a Go function that returns two integers and want to print both values using string formatting within a fmt.Println() call. However, this approach is not supported by default in Go.
Solution:
While you can't directly pass multiple return values to fmt.Println(), you can use a trick to achieve the same result with fmt.Printf():
Here's an example:
func wrap(vs ...interface{}) []interface{} { return vs } func twoInts() (int, int) { return 1, 2 } func main() { fmt.Printf("first= %d and second = %d", wrap(twoInts()...)...) }
This approach allows you to pass multiple return values to a variadic function, enabling you to print them using string formatting within fmt.Printf().
Note:
The above is the detailed content of How to Pass Multiple Return Values to a Variadic Function in Go?. For more information, please follow other related articles on the PHP Chinese website!