Created
January 27, 2022 19:54
-
-
Save percybolmer/eea359a86056daa16532bafea0400855 to your computer and use it in GitHub Desktop.
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(){ | |
// John is travelling to his Job | |
// His car travel is counted in int | |
// And his walking in Float32 | |
p := Person[float64]{Name: "John"} | |
c := Car[int]{Name: "Ferrari"} | |
// John has 100 miles to his job | |
milesToDestination := 100 | |
// John moves with the Car | |
distanceLeft := Move(c, milesToDestination, 95) | |
// John has 5 miles left to walk after parking (phew) | |
fmt.Println(distanceLeft) | |
fmt.Println("DistanceType: ", reflect.TypeOf(distanceLeft)) | |
// Jumps out of Car and Walks to Building | |
// Again we need to define the data type to use for the Move function, or else it will default to int | |
// So here we have to tell Move to initialize with a Person type, with a float64 value, | |
// And that the Subtract data type is also float64 | |
// [Move[float64], float64] | |
// distanceLeft is also a INT, since Move defaulted to int in previous call, so we need to convert it | |
newDistanceLeft := Move[Person[float64], float64](p, float64(distanceLeft), 5) | |
fmt.Println(newDistanceLeft) | |
fmt.Println("DistanceType: ", reflect.TypeOf(newDistanceLeft)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment