From a1877bcd5f7ca652d8914988811a3150a8fc59b1 Mon Sep 17 00:00:00 2001 From: Steven Degutis Date: Fri, 16 Mar 2012 07:17:07 -0500 Subject: [PATCH] varargs --- about_variadic_functions.go | 20 ++++++++++++++++++++ setup_koans_test.go | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 about_variadic_functions.go diff --git a/about_variadic_functions.go b/about_variadic_functions.go new file mode 100644 index 0000000..c6f74ff --- /dev/null +++ b/about_variadic_functions.go @@ -0,0 +1,20 @@ +package go_koans + +import "strings" + +func concatNames(sep string, names ...string) string { + return strings.Join(names, sep) // variadic parameters are really just slices +} + +func testVariadicFunctions() { + { + str := concatNames(" ", "bob", "billy", "fred") + assert(str == __string__) // several values can be passed to variadic parameters + } + + { + names := []string{"bob", "billy", "fred"} + str := concatNames("-", names...) + assert(str == __string__) // or a slice can be dotted in place of all of them + } +} diff --git a/setup_koans_test.go b/setup_koans_test.go index 03f9b10..cec612f 100644 --- a/setup_koans_test.go +++ b/setup_koans_test.go @@ -23,11 +23,11 @@ func TestKoans(t *testing.T) { //testSlices() //testControlFlow() //testEnumeration() - testAnonymousFunctions() + //testAnonymousFunctions() + testVariadicFunctions() // TODO: ie, gameplan //testFiles() - //testVariadicFunctions() //testInterfaces() //testMaps() //testStructs()