From 416811ed8b9325e8961536eaf70227d7a2c8f74e Mon Sep 17 00:00:00 2001 From: Fernando Lopes <118869201+plasmus777@users.noreply.github.com> Date: Tue, 23 Apr 2024 18:29:21 -0300 Subject: [PATCH] Generics --- learning/interfaces/interfaces.go | 5 --- learning/main.go | 7 +++- learning/simple/generics.go | 35 +++++++++++++++++ learning/simple/interfaces.go | 65 +++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 7 deletions(-) delete mode 100644 learning/interfaces/interfaces.go create mode 100644 learning/simple/generics.go create mode 100644 learning/simple/interfaces.go diff --git a/learning/interfaces/interfaces.go b/learning/interfaces/interfaces.go deleted file mode 100644 index 0ecf0b6..0000000 --- a/learning/interfaces/interfaces.go +++ /dev/null @@ -1,5 +0,0 @@ -package interfaces - -func Interfaces() { - -} diff --git a/learning/main.go b/learning/main.go index c1c6654..c40dd6c 100644 --- a/learning/main.go +++ b/learning/main.go @@ -6,7 +6,6 @@ import ( "github.com/plasmus777/go_lang/learning/arrays" "github.com/plasmus777/go_lang/learning/fluxcontrol" "github.com/plasmus777/go_lang/learning/inheritance" - "github.com/plasmus777/go_lang/learning/interfaces" "github.com/plasmus777/go_lang/learning/simple" ) @@ -36,6 +35,7 @@ func main() { fmt.Println("14 - structs") fmt.Println("15 - inheritance") fmt.Println("16 - interfaces") + fmt.Println("17 - generics") fmt.Println("") fmt.Println("-1 - exit") @@ -90,7 +90,10 @@ func main() { inheritance.Inheritance() case 16: - interfaces.Interfaces() + simple.Interfaces() + + case 17: + simple.Generics() case -1: fmt.Println("Exiting program.") diff --git a/learning/simple/generics.go b/learning/simple/generics.go new file mode 100644 index 0000000..848ecab --- /dev/null +++ b/learning/simple/generics.go @@ -0,0 +1,35 @@ +package simple + +import "fmt" + +func Generics() { + slice1 := []int{5, 3, 7, 1} + fmt.Println(slice1) + fmt.Println(*reverse(slice1)) + + fmt.Println() + + slice2 := []string{"b", "g", "a", "m"} + fmt.Println(slice2) + fmt.Println(*reverse(slice2)) + +} + +//Creating custom constraints can make your code easier to read/understand. +type customConstraint interface { + int | string +} + +//Generics are useful when you want to reuse code dealing with different argument/return types. +//T is a generic type (in this case, either an int or a string). +func reverse[T customConstraint](slice []T) *[]T { + reverseSlice := make([]T, len(slice)) + + j := 0 + for i := len(slice) - 1; i >= 0; i-- { + reverseSlice[j] = slice[i] + j++ + } + + return &reverseSlice +} diff --git a/learning/simple/interfaces.go b/learning/simple/interfaces.go new file mode 100644 index 0000000..c9ceb9d --- /dev/null +++ b/learning/simple/interfaces.go @@ -0,0 +1,65 @@ +package simple + +import ( + "fmt" + "math" +) + +// Go does not require the usage of "implements" or similar terms - the implementation is implicit. +type Geometry interface { + Area() float64 +} + +func DisplayGeometryArea(g Geometry) { + fmt.Println("Area:", g.Area()) +} + +type Rectangle struct { + height, width float64 +} + +type Circle struct { + radius float64 +} + +func (r *Rectangle) Area() float64 { + return r.width * r.height +} + +func (c *Circle) Area() float64 { + return math.Pi * math.Pow(c.radius, 2) +} + +func Interfaces() { + r := Rectangle{ + height: 2, + width: 4, + } + + c := Circle{ + radius: 3, + } + + DisplayGeometryArea(&r) + DisplayGeometryArea(&c) + + fmt.Println() + + //The empty interface can have different types of value + var slice []interface{} + + slice = append(slice, 10) + slice = append(slice, 8.4) + slice = append(slice, true) + slice = append(slice, "test") + + fmt.Println(slice) + + for index, value := range slice { + if val, ok := value.(string); ok { + fmt.Println(val+" is a string; index =", index) + } else { + fmt.Println("index =", index) + } + } +}