8000 Added code for a new module which teaches generics: create_generics · anarchymonkey/learn_golang@8232750 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8232750

Browse files
committed
Added code for a new module which teaches generics: create_generics
1 parent ab52429 commit 8232750

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

create_generics/generics/generics.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"batsy.com/utils"
7+
)
8+
9+
func main() {
10+
fmt.Println("This is a simple example of a generics module")
11+
12+
integerValues := map[string]int{
13+
"first": 100,
14+
"second": 200,
15+
}
16+
17+
floatingValues := map[string]float64{
18+
"first": 100.20,
19+
"second": 200.80,
20+
}
21+
22+
// fmt.Println("as we can see that we need to make a lot of functions to get the same summation of numbers")
23+
// fmt.Println(utils.SumOfNIntegers(integerValues))
24+
// fmt.Println(utils.SumOfNFloats(floatingValues))
25+
26+
fmt.Println("Now by using generics we can use the same function for the calculation of every number")
27+
28+
fmt.Println(utils.SumOfNNumbers(integerValues))
29+
fmt.Println(utils.SumOfNNumbers(floatingValues))
30+
}

create_generics/generics/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module anarchymonkey.com/generics
2+
3+
go 1.20
4+
5+
replace batsy.com/utils => ../utils
6+
7+
require batsy.com/utils v0.0.0-00010101000000-000000000000

create_generics/go.work

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.20
2+
3+
use (
4+
./generics
5+
./utils
6+
)

create_generics/utils/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module batsy.com/utils
2+
3+
go 1.20

create_generics/utils/utils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package utils
2+
3+
func SumOfNIntegers(numbers map[string]int) int {
4+
var sum int = 0
5+
for _, num := range numbers {
6+
sum += num
7+
}
8+
9+
return sum
10+
}
11+
12+
func SumOfNFloats(numbers map[string]float64) float64 {
13+
14+
var sum float64 = 0.00
15+
16+
for _, num := range numbers {
17+
sum += num
18+
}
19+
return sum
20+
}
21+
22+
func SumOfNNumbers[K comparable, V int | float64](m map[K]V) V {
23+
var s V
24+
25+
for _, v := range m {
26+
s += v
27+
}
28+
29+
return s
30+
}

create_generics/utils/utils_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package utils
2+
3+
import (
4+
"log"
5+
"testing"
6+
)
7+
8+
func TestSumOfNIntegers(t *testing.T) {
9+
integerNumbers := map[string]int{
10+
"first": 10,
11+
"second": 20,
12+
}
13+
actual_result := SumOfNIntegers(integerNumbers)
14+
expected_result := 30
15+
16+
if actual_result != expected_result {
17+
log.Fatalf("Some error occured, wrong result")
18+
}
19+
}
20+
21+
func TestSumOfNFloats(t *testing.T) {
22+
23+
var floatingNumbers map[string]float64 = map[string]float64{
24+
"first": 10.01,
25+
"second": 20.09,
26+
}
27+
28+
actual_result := SumOfNFloats(floatingNumbers)
29+
30+
expected_result := 30.10
31+
32+
if actual_result != expected_result {
33+
log.Fatalf("Floating number results do not match")
34+
}
35+
36+
}
37+
38+
func TestSumOfNNumbers(t *testing.T) {
39+
integerNumbers := map[string]int{
40+
"first": 10,
41+
"second": 20,
42+
}
43+
44+
actual_result := SumOfNNumbers(integerNumbers)
45+
expected_result := 30
46+
47+
if actual_result != expected_result {
48+
log.Fatal("The results do not match")
49+
}
50+
}

0 commit comments

Comments
 (0)
0