Skip to content

Commit

Permalink
Example #5: generic print function
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Jun 13, 2022
1 parent 415b792 commit 3ca1ac1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lesson8/05_generic_print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Type parameters introduced to Go in version 1.18
// -> generic function!

// Now the function printValue accepts value of any type

// (in Scala - any is "top type", none is "bottom type")

package main

import "fmt"

func printValue[T any](value T) {
fmt.Println(value)
}

func main() {
printValue("www.root.cz")
printValue('*')
printValue(42)
printValue(3.14)
printValue(1 + 2i)
printValue([]int{1, 2, 3})
}

0 comments on commit 3ca1ac1

Please sign in to comment.