Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created January 9, 2022 21:18
Show Gist options
  • Save percybolmer/661b88414730bfa15d15bc1de26f151a to your computer and use it in GitHub Desktop.
Save percybolmer/661b88414730bfa15d15bc1de26f151a to your computer and use it in GitHub Desktop.
// Results is a array of results, the data types can be any
type Results[T any] []T
// Subtractable is a type constraint that defines subtractable datatypes to be used in generic functions
type Subtractable interface {
int | int32 | int64 | float32 | float64 | uint | uint32 | uint64
}
func main(){
var a int = 20
var b int = 10
result := Subtract(a, b)
var c float32 = 20.5
var d float32 = 10.3
result2 := Subtract[float32](c, d)
result3 := Subtract(c, d)
fmt.Println("Result: ", result)
fmt.Println("Result2: ", result2)
fmt.Println("Result3: ", result3)
// Create a generic Results type, and set the instantitation to int
var resultStorage Results[any]
// We can now append all values
resultStorage = append(resultStorage, result, result2, result3)
fmt.Println("ResultStorage: ", resultStorage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment