-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (35 loc) · 856 Bytes
/
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
package main
import "fmt"
func max(numbers ...int) int {
var largest int
for _, v := range numbers {
if v > largest {
largest = v
}
}
return largest
}
func main() {
greatest := max(4, 7, 9, 123, 543, 23, 435, 53, 125)
fmt.Println(greatest)
}
/*
FYI
For your code to also work with only negative numbers such as
greatest := max(-200 -700)
include this as your range statement
for i, v := range numbers {
if v > largest || i == 0 {
largest = v
}
}
What does that code do?
The first time through the range loop
the index, i, will be zero
so largest will be set to the first number
Originally largest is set to the zero value for an int, which is zero
Zero would be greater than any negative number
if you only have negative numbers
you need largest to be something less than zero
Thanks to Ricardo G for this code improvement!
*/