Skip to content

Commit

Permalink
Generics
Browse files Browse the repository at this point in the history
  • Loading branch information
plasmus777 committed Apr 23, 2024
1 parent 09aabb5 commit 416811e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
5 changes: 0 additions & 5 deletions learning/interfaces/interfaces.go

This file was deleted.

7 changes: 5 additions & 2 deletions learning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -90,7 +90,10 @@ func main() {
inheritance.Inheritance()

case 16:
interfaces.Interfaces()
simple.Interfaces()

case 17:
simple.Generics()

case -1:
fmt.Println("Exiting program.")
Expand Down
35 changes: 35 additions & 0 deletions learning/simple/generics.go
Original file line number Diff line number Diff line change
@@ -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
}
65 changes: 65 additions & 0 deletions learning/simple/interfaces.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 416811e

Please sign in to comment.