Golang: Passing a slice of variables to a function as multiple arguments

You need to pass some unknown number of values to a function (https://en.wikipedia.org/wiki/Variadic_function) which accepts unlimited arguments in golang? Put everything in a slice of interface{} and unpack that with "..."

id := 1
name := "test"

// What we want to output: 1 - test
fmt.Printf("%d - %s\n", id, name)
// Create a slice of interfaces containing the int id.
args := []interface{}{id}
// Adding name string separately to show how to do it.
args = append(args, name)
// Send args to Printf using triple dot notation.
fmt.Printf("%d - %s\n", args...)

It took me a longer than I thought it should to work out the correct method to implement this. So, I'm making a note of it here.


Thanks a lot Brad this was very useful. I was getting crazy with a similar problem and you turned the light in my head on!
great note!