Skip to content

Commit

Permalink
Example #12: type parameters + return value
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Jun 13, 2022
1 parent 0d4357d commit 434602b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lesson8/12_add_type_parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Generic "add" function for three distinct data types.

package main

import "fmt"

type numeric interface {
int | float64 | complex128
}

func add[T numeric](x T, y T) T {
return x + y
}

func main() {
fmt.Println(add(1, 2))
fmt.Println(add(1.5, 2.6))
fmt.Println(add(1i, 2+4i))
}

0 comments on commit 434602b

Please sign in to comment.