-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (39 loc) · 1.05 KB
/
main.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
package main
import "fmt"
// Unpack operator breaks a slice into individual components. it ends with ... like slice...
// Pack operator combine individual elements like integers into a slice. It starts with ... like ...Type
// Effective Go talks about append but wish it would spend just a bit more time on the subject.
var exNum int
func main() {
variadicA([]string{"a", "b", "c"}...)
variadicA("d", "e", "f")
// The mythical variadic interface{}
variadicInterface(1.0, "a", 2, []int{1, 2})
}
func variadicA(elems ...string) {
exNum++
fmt.Printf("Example number: %d", exNum)
str := ""
for _, e := range elems {
str += e
}
fmt.Println("Output: ", str)
}
func variadicInterface(elems ...interface{}) {
exNum++
fmt.Printf("Example number: %d", exNum)
for _, e := range elems {
switch e.(type) {
case float64:
fmt.Println("Type is float64")
case int:
fmt.Println("Type is Integer")
case string:
fmt.Println("Type is String")
case []int:
fmt.Println("Type is []int")
default:
fmt.Printf("Catch-all. Type is %T", e)
}
}
}