From 4349285b7b83cc6595641baf413b33f112919825 Mon Sep 17 00:00:00 2001 From: asandikci Date: Fri, 25 Aug 2023 19:09:21 +0300 Subject: [PATCH] 4 Generics --- 4generics/generic-types.go | 26 ++++++++++++ 4generics/type-paramaters.go | 78 ++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 4generics/generic-types.go create mode 100644 4generics/type-paramaters.go diff --git a/4generics/generic-types.go b/4generics/generic-types.go new file mode 100644 index 0000000..701a387 --- /dev/null +++ b/4generics/generic-types.go @@ -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() ... +} diff --git a/4generics/type-paramaters.go b/4generics/type-paramaters.go new file mode 100644 index 0000000..458a7ba --- /dev/null +++ b/4generics/type-paramaters.go @@ -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)) + +}