4 Generics
This commit is contained in:
parent
3a5b8b778a
commit
4349285b7b
2 changed files with 104 additions and 0 deletions
26
4generics/generic-types.go
Normal file
26
4generics/generic-types.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// List represents a singly-linked list that holds
|
||||
// values of any type.
|
||||
type List[T any] struct {
|
||||
next *List[T]
|
||||
val T
|
||||
}
|
||||
|
||||
func main() {
|
||||
list := List[any]{}
|
||||
list.next = &List[any]{}
|
||||
list.val = 123
|
||||
list.next.val = "hello"
|
||||
|
||||
fmt.Println(list.val)
|
||||
fmt.Println(list.next.val)
|
||||
fmt.Println(list)
|
||||
|
||||
// NEXT complete linked list implementation
|
||||
// single linked list
|
||||
// doubly linked list
|
||||
// addNode(), addNodeAt(), deleteNode(), deleteNodeAt(), getList() ...
|
||||
}
|
78
4generics/type-paramaters.go
Normal file
78
4generics/type-paramaters.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
// extra resource: https://go.dev/doc/tutorial/generics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// interfaces with type elements couldn't be used anywhere other than type paramaters
|
||||
type Number interface {
|
||||
int64 | float64
|
||||
}
|
||||
|
||||
type Myint interface {
|
||||
int64
|
||||
}
|
||||
|
||||
func SumInt[K comparable, V Myint](m map[K]V) V {
|
||||
var s V
|
||||
for _, v := range m {
|
||||
s += v
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func SumIntFloat[K comparable, V Number](m map[K]V) V {
|
||||
var s V
|
||||
for _, v := range m {
|
||||
s += v
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func SumAll[K comparable, V constraints.Ordered](m map[K]V) V {
|
||||
var s V
|
||||
for _, v := range m {
|
||||
s += v
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func main2() { // change main2 to main befor testing !!!
|
||||
fmt.Println("Go functions can be written to work on multiple types using type parameters")
|
||||
fmt.Println("square bracked types after function names are type paramaters")
|
||||
fmt.Println("You can create type paramaters with keyword `comparable`(for comparable types), `any` for any type or other type name/s.")
|
||||
fmt.Println("See differences between SumInt, SumIntFloat and SumAll")
|
||||
fmt.Println("Please note down map keys should be comparable because of default map behaviour")
|
||||
|
||||
ints := map[string]int64{
|
||||
"first": 1,
|
||||
"second": 2,
|
||||
"third": 3,
|
||||
}
|
||||
|
||||
floats := map[string]float64{
|
||||
"first": 1.23,
|
||||
"second": 2.3123,
|
||||
"third": 3,
|
||||
}
|
||||
|
||||
strings := map[string]string{
|
||||
"first": "str1",
|
||||
"second": "str2",
|
||||
"third": "str3",
|
||||
}
|
||||
|
||||
fmt.Println(SumInt(ints))
|
||||
// fmt.Println(SumInt(floats)) // error
|
||||
fmt.Println(SumIntFloat(ints))
|
||||
fmt.Println(SumIntFloat(floats))
|
||||
// fmt.Println(SumIntFloat(strings)) // error
|
||||
fmt.Println(SumAll(ints))
|
||||
fmt.Println(SumAll(floats))
|
||||
fmt.Println(SumAll(strings))
|
||||
|
||||
}
|
Loading…
Reference in a new issue