Created
August 9, 2022 14:35
-
-
Save tsyber1an/594ff022bf878e91080e07de33a64fe9 to your computer and use it in GitHub Desktop.
Go 1.18 Generics usage example
This file contains 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 handler[arg int64 | float32](input arg) { | |
fmt.Println(input) | |
} | |
type Number in qzzzterface { | |
int8 | int16 | int32 | int64 | float32 | float64 | |
} | |
func BubbleSort[N Number](input []N) []N { | |
n := len(input) | |
swapped := true | |
for swapped { | |
swapped = false | |
for i := 0; i < n-1; i++ { | |
if input[i] > input[i+1] { | |
input[i], input[i+1] = input[i+1], input[i] | |
swapped = true | |
} | |
} | |
} | |
return input | |
} | |
func main() { | |
var a int64 = 160 | |
var b float32 = 1.3 | |
handler(a) | |
handler(b) | |
list := []int32{4,3,1, 5,6} | |
lstFloat := []float32{4.3, 3.2, 1.3, 5.1, 6.9} | |
sorted := BubbleSort(list) | |
fmt.Println(sorted) | |
sortedFloats := BubbleSort(lstFloat) | |
fmt.Println(sortedFloats) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment