-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter11.go
76 lines (63 loc) · 1.6 KB
/
chapter11.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"math"
)
func main() {
problem1()
problem2()
problem3()
}
func problem1() {
fmt.Println("Packages are used to help you organize your code, and because they make it easier to reuse your code.")
}
func problem2() {
fmt.Println(`Functions with a capital letter are public, those with a lowercase letter are not private; meaning that
other packages cannot use it.`)
}
func problem3() {
fmt.Println(`A package alias lets you refer to a package with a shorter name.
For example, say the full name of your package is: "golang/chapter11/samplepackage", then you can import it as:
import sp "golang/chapter11/samplepackage"
and now when you refer to functions inside of that package, you can just do:
sp.<function>`)
}
// Begin problem 4 below, after Average function
// Func was taken from page 122
func Average(xs []float64) float64 {
total := float64(0)
for _, x := range xs {
total += x
}
return total / float64(len(xs))
}
// Finds the minimum of a slice of integers
func Min(xs []int8) int8 {
if (len(xs) != 0){
minimum := int8(xs[0])
} else {
minimum := math.MaxInt8
}
for _, item := range xs {
if minimum < item {
minimum = item
}
}
return minimum
}
// Finds the maximum of a slice of integers
func Max(xs []int8) int8 {
if len(xs) != 0{
maximum := int8(xs[0])
} else {
maximum := math.MinInt8
}
for _, item := range xs {
if maximum > item {
maximum = item
}
}
return maximum
}
// The answer for problem #5 is the documentation for the two functions above.
// Sadly the godoc features don't work on CentOS Linux release 7.4.1708 (Core)