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
package main | |
import ( | |
"fmt" | |
"reflect" | |
) | |
// 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 |
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
package main | |
import ( | |
"fmt" | |
"reflect" | |
) | |
// 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 |
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
// Moveable is a interface that is used to handle many objects that are moveable | |
type Moveable interface { | |
Move(int) | |
} | |
// Person is a person, implements Moveable | |
type Person struct { | |
Name string | |
} | |
func (p Person) Move(meters int) { |
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
// Move is a generic function that takes in a Moveable and moves it | |
func Move[V Moveable](v V, meters int) { | |
v.Move(meters) | |
} |
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
// 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 |
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 | |
result := Subtract(a, b) | |
var c float32 = 20.5 | |
var d float32 = 10.3 | |
result2 := Subtract[float32](c, d) |
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
// Results is a array of results, reusing the type constraint Subtractable | |
type Results[T Subtractable] []T |
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
var c float32 = 20.5 | |
var d float32 = 10.3 | |
result := Subtract(c, d) | |
result2 := Subtract[float32](c, d) |
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
package main | |
import ( | |
"fmt" | |
) | |
// 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 | |
} |
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
package main | |
import ( | |
"fmt" | |
) | |
func main(){ | |
var a int = 20 | |
var b int = 10 | |
// the compiler will infere the type used |