-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvariadic.go
49 lines (42 loc) · 1004 Bytes
/
variadic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"os"
)
func addFloats(message string, s ...float64) float64 {
fmt.Println(message)
sum := float64(0)
for _, a := range s {
sum = sum + a
}
s[0] = -1000
return sum
}
func everything(input ...interface{}) {
fmt.Println(input)
}
func main() {
sum := addFloats("Adding numbers...", 1.1, 2.12, 3.14, 4, 5, -1, 10)
fmt.Println("Sum:", sum)
s := []float64{1.1, 2.12, 3.14}
sum = addFloats("Adding numbers...", s...)
fmt.Println("Sum:", sum)
everything(s)
// Cannot directly pass []string as []interface{}
// You have to convert it first!
empty := make([]interface{}, len(os.Args[1:]))
for i, v := range os.Args[1:] {
empty[i] = v
}
everything(empty...)
// There is a slightly different way to do the conversion
arguments := os.Args[1:]
empty = make([]interface{}, len(arguments))
for i := range arguments {
empty[i] = arguments[i]
}
everything(empty...)
// This will work!
str := []string{"One", "Two", "Three"}
everything(str, str, str)
}