File tree Expand file tree Collapse file tree 6 files changed +126
-0
lines changed Expand file tree Collapse file tree 6 files changed +126
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ go 1.20
2
+
3
+ use (
4
+ ./generics
5
+ ./utils
6
+ )
Original file line number Diff line number Diff line change
1
+ module batsy.com/utils
2
+
3
+ go 1.20
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments