Skip to content

Commit e45d069

Browse files
committed
Tutorial10 - Generics
1 parent 556baed commit e45d069

File tree

5 files changed

+185
-1
lines changed

5 files changed

+185
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ go run tutorial1/main.go
5050
- [x] [Structs and Interfaces](tutorial6)
5151
- [x] [Pointers](tutorial7)
5252
- [x] [Concurrency(Goroutines)](tutorial8)
53-
- [x] [Channels](tutorial9)
53+
- [x] [Channels](tutorial9)
54+
- [x] [Generics](tutorial10)

tutorial10/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Generics in GO
2+
3+
## Key Concepts
4+
5+
### Generics
6+
7+
Generics is a concept in programming where a function or a data type can be written in a way that it can handle different data types. In Go, generics are defined using square brackets `[]` followed by a type parameter list.
8+
9+
### Generic Functions
10+
11+
In this program, we have three generic functions: `sumSlice`, `isEmpty`, and `LoadJSON`.
12+
13+
```go
14+
func sumSlice[T int | float32 | float64](slice []T) T{
15+
var sum T
16+
for _, v := range slice{
17+
sum += v
18+
}
19+
return sum
20+
}
21+
22+
func isEmpty[T any](slice []T) bool{
23+
return len(slice) == 0
24+
}
25+
26+
func LoadJSON[T contactInfo | purchaseInfo] (filePath string) []T{
27+
data, _ := ioutil.ReadFile(filePath)
28+
var loaded = []T{}
29+
json.Unmarshal(data, &loaded)
30+
31+
return loaded
32+
}
33+
```
34+
35+
`sumSlice` takes a slice of type `T` where `T` can be `int`, `float32`, or `float64`. It returns the sum of all elements in the slice.
36+
37+
`isEmpty` takes a slice of any type `T` and returns a boolean indicating whether the slice is empty.
38+
39+
`LoadJSON` takes a file path as input and returns a slice of type `T` where `T` can be `contactInfo` or `purchaseInfo`. It reads the JSON file from the given file path and unmarshals the data into a slice of the appropriate type.
40+
41+
### Generic Structs
42+
43+
In this program, we have two struct types: `contactInfo` and `purchaseInfo`.
44+
45+
```go
46+
type contactInfo struct{
47+
Name string
48+
Email string
49+
}
50+
51+
type purchaseInfo struct{
52+
Name string
53+
Price float32
54+
Amount int
55+
}
56+
```
57+
58+
These structs are used as type parameters in the `LoadJSON` function to load JSON data of different types.
59+
60+
## Usage
61+
62+
In the `main` function, we demonstrate the usage of these generic functions.
63+
64+
```go
65+
func main(){
66+
var intSlice = []int{1,2,3}
67+
fmt.Println(sumSlice[int](intSlice))
68+
69+
var float32Slice = []float32{1,2,3}
70+
fmt.Println(sumSlice[float32](float32Slice))
71+
72+
var sliceint = []int{}
73+
fmt.Println(isEmpty[int](sliceint))
74+
fmt.Println(isEmpty[int](intSlice))
75+
76+
var contacts []contactInfo = LoadJSON[contactInfo]("./files/contactInfo.json")
77+
fmt.Printf("\n%+v", contacts)
78+
79+
var purchases []purchaseInfo = LoadJSON [purchaseInfo]("./files/PurchaseInfo.json")
80+
fmt.Printf("\n%+v", purchases)
81+
}
82+
```
83+
84+
We create slices of `int` and `float32` and use `sumSlice` to calculate their sums. We also check if a slice is empty using `isEmpty`. Finally, we use `LoadJSON` to load JSON data into slices of `contactInfo` and `purchaseInfo`.
85+
86+
### Checkout the code
87+
88+
- [main.go](main.go)

tutorial10/files/contactInfo.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"name": "Alex",
4+
"Email": "sp@gmail.com"
5+
},
6+
{
7+
"name": "Jason",
8+
"number": "jas@gmail.com"
9+
}
10+
]

tutorial10/files/purchaseInfo.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"name": "Alex",
4+
"price": 19.99,
5+
"amount": 3
6+
},
7+
{
8+
"name": "Jason",
9+
"price": 29.99,
10+
"amount": 1
11+
}
12+
]

tutorial10/main.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"encoding/json"
6+
"io/ioutil"
7+
)
8+
9+
type contactInfo struct{
10+
Name string
11+
Email string
12+
}
13+
14+
type purchaseInfo struct{
15+
Name string
16+
Price float32
17+
Amount int
18+
}
19+
20+
func main(){
21+
var intSlice = []int{1,2,3}
22+
fmt.Println(sumSlice[int](intSlice))
23+
24+
var float32Slice = []float32{1,2,3}
25+
fmt.Println(sumSlice[float32](float32Slice))
26+
27+
var sliceint = []int{}
28+
fmt.Println(isEmpty[int](sliceint))
29+
fmt.Println(isEmpty[int](intSlice))
30+
31+
var contacts []contactInfo = LoadJSON[contactInfo]("./files/contactInfo.json")
32+
fmt.Printf("\n%+v", contacts)
33+
34+
var purchases []purchaseInfo = LoadJSON [purchaseInfo]("./files/PurchaseInfo.json")
35+
fmt.Printf("\n%+v", purchases)
36+
}
37+
38+
func sumSlice[T int | float32 | float64](slice []T) T{
39+
var sum T
40+
for _, v := range slice{
41+
sum += v
42+
}
43+
return sum
44+
}
45+
46+
// we can do type any because not all types are compaitible with + operators
47+
48+
func isEmpty[T any](slice []T) bool{
49+
return len(slice) == 0
50+
}
51+
52+
func LoadJSON[T contactInfo | purchaseInfo] (filePath string) []T{
53+
data, _ := ioutil.ReadFile(filePath)
54+
var loaded = []T{}
55+
json.Unmarshal(data, &loaded)
56+
57+
return loaded
58+
}
59+
60+
/*
61+
>>>>>>>>>>>>>>>>>>>>>>> go run tutorial10/main.go
62+
63+
O/P :::::::::::::::::::
64+
65+
6
66+
6
67+
true
68+
false
69+
70+
[]
71+
[]
72+
PS
73+
*/

0 commit comments

Comments
 (0)