Skip to content

Instantly share code, notes, and snippets.

@tlehman
Last active February 21, 2022 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlehman/2d68a61d3945949485668b711251ad70 to your computer and use it in GitHub Desktop.
Save tlehman/2d68a61d3945949485668b711251ad70 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func double[T int | float64](x T) T {
return T(2) * x
}
func main() {
var a int = 5
var adbl = double(a)
var b float64 = 5.5
var bdbl = double(b)
fmt.Printf("adbl = %d, bdbl = %f\n", adbl, bdbl)
}
@tlehman
Copy link
Author

tlehman commented Feb 21, 2022

tobi@ark:~/tmp> go version
go version go1.18rc1 linux/amd64
tobi@ark:~/tmp> ./generics 
adbl = 10, bdbl = 11.000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment