-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (139 loc) · 3.51 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import "fmt"
func testSwitchCases(a int){
switch a{
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
default:
fmt.Println("Other")
}
}
func testSwitchCasesWithInterface(a interface{}){
switch types := a.(type){
case string:
fmt.Println(types, "is string")
case int:
fmt.Println(types, "is int")
default:
fmt.Println(types, "is other")
}
}
//You could use more specific constraints, e.g., [T ~int | ~string] to allow only int-like or string-like types.
// any is a type alias for interface{}.
func testSwitchWithGenerics[T any](a T) {
if str, ok := any(a).(string); ok {
fmt.Println(str, "is string")
} else if num, ok := any(a).(int); ok {
fmt.Println(num, "is int")
} else {
fmt.Println(a, "is other")
}
}
func testSwitchWithGenerics2[T any](a T) {
switch v := any(a).(type) {
case string:
fmt.Println(v, "is string")
case int:
fmt.Println(v, "is int")
default:
fmt.Println(v, "is other")
}
}
// New generic functions to explain generics further
// Generic function to print any slice
func PrintSlice[T any](s []T) {
fmt.Print("Slice contents: ")
for _, v := range s {
fmt.Printf("%v ", v)
}
fmt.Println()
}
// Generic function to find the maximum value in a slice
func Max[T int | float64](s []T) T {
if len(s) == 0 {
var zero T
return zero
}
max := s[0]
for _, v := range s[1:] {
if v > max {
max = v
}
}
return max
}
// Generic Stack implementation
type Stack[T any] struct {
items []T
}
func (s *Stack[T]) Push(item T) {
s.items = append(s.items, item)
}
func (s *Stack[T]) Pop() (T, bool) {
if len(s.items) == 0 {
var zero T
return zero, false
}
item := s.items[len(s.items)-1]
s.items = s.items[:len(s.items)-1]
return item, true
}
func main() {
//below will cause a runtime error because the map is not initialized
// var m map[string]int
// m["a"] = 1
// fmt.Println(m)
//below will not cause a runtime error because the map is initialized with a literal. Literal is a value that is known at compile time. Example: var a = 1
var m = map[string]int{}
m["a"] = 1
fmt.Println(m)
//A sample code using switch with channel
c := make(chan int)
go func() {
c <- 1
}()
select {
case <-c:
fmt.Println("Received from channel")
}
myMap := map[string]int{"a": 1, "b": 2, "c": 3}
if value, exists := myMap["key"]; exists {
fmt.Println("Value found:", value)
} else {
fmt.Println("Key not found in map")
}
testSwitchCases(3)
testSwitchCasesWithInterface(true)
testSwitchWithGenerics(1)
testSwitchWithGenerics2(true)
a := [3]int{1,2,3}
func(a *[3]int){
a[0]= 4
}(&a)
fmt.Println(a)
// Demonstrating new generic functions
intSlice := []int{1, 5, 3, 7, 2}
floatSlice := []float64{1.1, 5.5, 3.3, 7.7, 2.2}
stringSlice := []string{"apple", "banana", "cherry"}
PrintSlice(intSlice)
PrintSlice(floatSlice)
PrintSlice(stringSlice)
fmt.Printf("Max of intSlice: %d\n", Max(intSlice))
fmt.Printf("Max of floatSlice: %.2f\n", Max(floatSlice))
// Using generic Stack
intStack := &Stack[int]{}
intStack.Push(1)
intStack.Push(2)
intStack.Push(3)
if val, ok := intStack.Pop(); ok {
fmt.Printf("Popped from intStack: %d\n", val)
}
stringStack := &Stack[string]{}
stringStack.Push("Hello")
stringStack.Push("World")
if val, ok := stringStack.Pop(); ok {
fmt.Printf("Popped from stringStack: %s\n", val)
}
}