Permalink
Browse files

publish variadic functions. also, spelling

  • Loading branch information...
1 parent 855fde2 commit 22f6542915c188804d9128dd1ef62603ca327f96 @mmcgrana committed Oct 18, 2012
View
2 examples.txt
@@ -11,7 +11,7 @@ Maps
Range
Functions
Multiple Return Values
-# Varadic Functions
+Variadic Functions
Closures
Recursion
# Defer
View
4 examples/multiple-return-values/multiple-return-values.sh
@@ -3,5 +3,5 @@ $ go run multiple-return-values.go
7
7
-# Another key aspect of functions in Go is their ability
-# to form closures, which we'll look at next.
+# Accepting a variable number of arguments is another nice
+# feature of Go functions; we'll look at this next.
View
2 examples/redis/redis.go
@@ -25,7 +25,7 @@ func main() {
fmt.Println(getRep)
fmt.Println(getStr)
- // varadic calls
+ // variadic calls
client.Set("foo1", "bar1")
client.Set("foo2", "bar2")
client.Set("foo3", "bar3")
View
31 examples/varadic-functions/varadic-functions.go
@@ -1,31 +0,0 @@
-// Varadic functions can be called with any number of
-// trailing arguments. This is useful if you don't know
-// number of arguments that will be needed for a function
-// ahead of time.
-
-package main
-
-import "fmt"
-
-// Varadic args are declared with `...type` and
-// passed in as a slice.
-func add(nums ...int) int {
- fmt.Print(nums, " ")
- total := 0
- for _, num := range nums {
- total += num
- }
- return total
-}
-
-func main() {
- // Varadic functions can be called in the usual way.
- fmt.Println(add(1, 2))
- fmt.Println(add(1, 2, 3))
-
- // If you already have multiple args in a slice,
- // apply them to a varadic function using `
- // func(slice...)`.
- nums := []int{1, 2, 3, 4}
- fmt.Println(add(nums...))
-}
View
4 examples/varadic-functions/varadic-functions.sh
@@ -1,4 +0,0 @@
-$ go run varadic-functions.go
-[1 2] 3
-[1 2 3] 6
-[1 2 3 4] 10
View
33 examples/variadic-functions/variadic-functions.go
@@ -0,0 +1,33 @@
+// [_Variadic functions_](http://en.wikipedia.org/wiki/Variadic_function)
+// can be called with any number of trailing arguments.
+// For example, `fmt.Println` is a common variadic
+// function.
+
+package main
+
+import "fmt"
+
+// Here's a function that will take an arbitrary number
+// of `ints` as arguments.
+func sum(nums ...int) int {
+ fmt.Print(nums, " ")
+ total := 0
+ for _, num := range nums {
+ total += num
+ }
+ return total
+}
+
+func main() {
+
+ // Variadic functions can be called in the usual way
+ // with individual arguments.
+ fmt.Println(sum(1, 2))
+ fmt.Println(sum(1, 2, 3))
+
+ // If you already have multiple args in a slice,
+ // apply them to a variadic function using
+ // `func(slice...)` like this.
+ nums := []int{1, 2, 3, 4}
+ fmt.Println(sum(nums...))
+}
View
7 examples/variadic-functions/variadic-functions.sh
@@ -0,0 +1,7 @@
+$ go run variadic-functions.go
+[1 2] 3
+[1 2 3] 6
+[1 2 3 4] 10
+
+# Another key aspect of functions in Go is their ability
+# to form closures, which we'll look at next.

0 comments on commit 22f6542

Please sign in to comment.