-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
59 lines (42 loc) · 772 Bytes
/
functions.go
File metadata and controls
59 lines (42 loc) · 772 Bytes
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
50
51
52
53
54
55
56
57
58
59
package main
import "fmt"
func plus(a int, b int) int {
return a + b
}
func vals() (int, int) {
return 3, 5
}
// variable length args
func sum(nums ...int) int {
total := 0
for i := range len(nums) {
total += nums[i]
}
return total
}
func FunctionProgram() {
result := plus(2, 3)
fmt.Println(result)
a, b := vals() // we don't want a value we can use _
fmt.Println(a, b)
nums := []int{3, 4, 5, 6}
fmt.Println(sum(nums...))
// 1. ANONYMOUS FUNCTIONS
hello := func() {
fmt.Println("Hello there!")
}
// 2.
func(msg string) {
fmt.Println(msg)
}("HELLO GO")
hello()
// Anonymous recursive functions
var fib func(n int) int
fib = func(n int) int {
if n < 2 {
return n
}
return fib(n-1) + fib(n-2)
}
fmt.Println(fib(10))
}