# Golang ## Variadic Functions ^[Cloud Native Go, page 53] ```go func Printf(format string, a ...interface{}) (n int, err error) const name, age = "Kim", 22 fmt.Printf("%s is %d years old.\n", name, age) ``` ```go func product(factors ...int) int { p := 1 for _, n := range factors { p *= n } return p } func main(){ fmt.Println(product(2, 2, 2)) // 8 } ``` ## Anonymous Functions & Closures ^[Cloud Native Go, page 55] ```go func sum(x, y int) int { return x + y } func product(x, y int) int { return x * y } func main() { var f func(int, int) int f = sum fmt.Println(f(3, 5)) // "8" f = product // "legal" because product has same type of sum fmt.Println(f(3, 5)) // "15" } ``` ```go func incrementer() func() int { i := 0 return func() int { // returns an anonymous function i++ // "Closes over" parent function's i return i } } func main() { increment := incrementor() fmt.Println(increment()) // "1" fmt.Println(increment()) // "2" } ``` ## Type Assertions ^[Cloud Native Go, page 60] ```go var s Shape s = Circle{} c := s.(Circle) // Asserts that s is a circle fmt.Printf("%T\n",c) // "main.Circle" ```