Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created January 9, 2022 13:29
Show Gist options
  • Save percybolmer/a6d8b48e9cde1f38f971fcfc6c49c762 to your computer and use it in GitHub Desktop.
Save percybolmer/a6d8b48e9cde1f38f971fcfc6c49c762 to your computer and use it in GitHub Desktop.
A generic subtract function
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