From 1ab48c6a041ab614db2822b8f3e790a984e86247 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Mon, 13 Jun 2022 08:50:50 +0200 Subject: [PATCH] Example #9: compare for various types (no overloading) --- lesson8/09_comparable_variable_types.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lesson8/09_comparable_variable_types.go diff --git a/lesson8/09_comparable_variable_types.go b/lesson8/09_comparable_variable_types.go new file mode 100644 index 0000000..2ab5814 --- /dev/null +++ b/lesson8/09_comparable_variable_types.go @@ -0,0 +1,24 @@ +// In Go version < 1.18 it was not easy to make variant +// of such function for multiple data types. + +package main + +import "fmt" + +func compareInts(x int, y int) bool { + return x < y +} + +func compareFloats(x float64, y float64) bool { + return x < y +} + +func compareStrings(x string, y string) bool { + return x < y +} + +func main() { + fmt.Println(compareInts(1, 2)) + fmt.Println(compareFloats(1.5, 2.6)) + fmt.Println(compareStrings("foo", "bar")) +}