Skip to content

Commit

Permalink
Added some examples of generics in golang
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Oct 14, 2024
1 parent 3ead36b commit 263ddea
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions examples/golang/04c_generics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Generics from go 1.18

package main

import (
"fmt"
)

// Type set, Contracts befor 1.18
type Num interface {
int | float64
}

func Summa[T Num](a, b T) T {
return a + b
}

//func Concat[T any, U any](first T, second U) {
func Concat[T any, U any](first T, second U) {
fmt.Println(first, second)
}

func Distinct[T comparable](list []T) []T {
uniq := make(map[T]bool)
var result []T

for _, i := range list {
if !uniq[i] {
uniq[i] = true
result = append(result, i)
}
}
return result
}

func PrintAny(items []any) {
for _, item := range items {
fmt.Printf("%v ", item)
}
fmt.Println("")
}


func main() {
fmt.Println(Summa(5, 4))
fmt.Println(Summa(3.3, 8.1))
// fmt.Println(Summa("something", "in the water"))

Concat(22, "Building number")

fmt.Println(Distinct([]int{1, 4, 4, 6, 6, 6, 7, 9}))
fmt.Println(Distinct([]string{"word", "word", "cat", "war", "dog", "people"}))

PrintAny([]any{1, "some", true, 6.28})

}

0 comments on commit 263ddea

Please sign in to comment.