Created
January 9, 2022 13:29
-
-
Save percybolmer/a6d8b48e9cde1f38f971fcfc6c49c762 to your computer and use it in GitHub Desktop.
A generic subtract function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main(){ | |
var a int = 20 | |
var b int = 10 | |
var c float32 = 20 | |
var d float32 = 10.5 | |
result := Subtract(a, b) | |
// Here we tell the function that the input is a float32 data type, so expect the output parameter to be float32 | |
resultfloat := Subtract[float32](c, d) | |
// Will output 10 | |
fmt.Println("Result:", result) | |
// Will output 9.5 | |
fmt.Println("Resultfloat:", resultfloat) | |
} | |
// Subtract will subtract the second value from the first | |
func Subtract[V int64 | int | float32](a, b V) V { | |
return a - b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment