-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbasic-functions.go
More file actions
52 lines (40 loc) · 1 KB
/
basic-functions.go
File metadata and controls
52 lines (40 loc) · 1 KB
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
package main
import (
"fmt"
"math/rand"
"strconv"
"time"
)
type dieRollFunc func(int) int
func dieRoll(size int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(size) + 1
}
func fakeDieRoll(size int) int {
return 42
}
func rollTwo(size1, size2 int) (int, int) {
return dieRoll(size1), dieRoll(size2)
}
func getDieRolls() []dieRollFunc {
return []dieRollFunc{
dieRoll,
fakeDieRoll,
}
}
func returnsNamed(input1 string, input2 int) (theResult string, err error) {
theResult = "modified " + input1 + ", " + strconv.Itoa(input2)
return theResult, err
}
func main() {
fmt.Printf("Rolled a die of size %d, result: %d\n", 6, dieRoll(6))
res1, res2 := rollTwo(6, 10)
fmt.Printf("Rolled a pair of dice (%d,%d), results: %d, %d\n",
6, 10, res1, res2)
named, err := returnsNamed("globule", 42)
fmt.Printf("Named params returned: '%s', %v\n", named, err)
var rolls = getDieRolls()
for index, rollFunc := range rolls {
fmt.Printf("Die Roll Attempt #%d, result: %d\n", index, rollFunc(10))
}
}