varargs
This commit is contained in:
20
about_variadic_functions.go
Normal file
20
about_variadic_functions.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,11 +23,11 @@ func TestKoans(t *testing.T) {
|
|||||||
//testSlices()
|
//testSlices()
|
||||||
//testControlFlow()
|
//testControlFlow()
|
||||||
//testEnumeration()
|
//testEnumeration()
|
||||||
testAnonymousFunctions()
|
//testAnonymousFunctions()
|
||||||
|
testVariadicFunctions()
|
||||||
|
|
||||||
// TODO: ie, gameplan
|
// TODO: ie, gameplan
|
||||||
//testFiles()
|
//testFiles()
|
||||||
//testVariadicFunctions()
|
|
||||||
//testInterfaces()
|
//testInterfaces()
|
||||||
//testMaps()
|
//testMaps()
|
||||||
//testStructs()
|
//testStructs()
|
||||||
|
|||||||
Reference in New Issue
Block a user