8000 changes 16 exercise solution 3 to account for only negative numbers · Abhicodeitout/GolangTraining@fea6fc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit fea6fc5

Browse files
committed
changes 16 exercise solution 3 to account for only negative numbers
1 parent b5c6a5f commit fea6fc5

File tree

2 files changed

+58
-0
lines changed
  • 16_exercise-solutions/03_variadic-greatest
  • 24_testing/00_under-development/01

2 files changed

+58
-0
lines changed

16_exercise-solutions/03_variadic-greatest/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,32 @@ func main() {
1616
greatest := max(4, 7, 9, 123, 543, 23, 435, 53, 125)
1717
fmt.Println(greatest)
1818
}
19+
20+
/*
21+
FYI
22+
For your code to also work with only negative numbers such as
23+
24+
greatest := max(-200 -700)
25+
26+
include this as your range statement
27+
for i, v := range numbers {
28+
if v > largest || i == 0 {
29+
largest = v
30+
}
31+
}
32+
33+
What does that code do?
34+
35+
The first time through the range loop
36+
the index, i, will be zero
37+
so largest will be set to the first number
38+
39+
Originally largest is set to the zero value for an int, which is zero
40+
41+
Zero would be greater than any negative number
42+
43+
if you only have negative numbers
44+
you need largest to be something less than zero
45+
46+
Thanks to Ricardo G for this code improvement!
47+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
main.go
2+
3+
package main
4+
5+
import "fmt"
6+
7+
func sup(name string) string {
8+
return "Sup, " + name
9+
}
10+
11+
func main() {
12+
fmt.Println(sup("Ewan"))
13+
}
14+
Now our tests in, main_test.go
15+
16+
package main
17+
18+
import "testing"
19+
20+
func TestSup(t *testing.T) {
21+
expected := "Sup, Ewan"
22+
outcome := sup("Ewan")
23+
if outcome != expected {
24+
t.Fatalf("Expected %s, got %s", expected, outcome)
25+
}
26+
}
27+
All I do now is run go test and I get...
28+
29+
http://ewanvalentine.io/why-go-solves-so-many-problems-for-web-developers/?utm_source=golangweekly&utm_medium=email

0 commit comments

Comments
 (0)
0