非泛型版本:
bubbleSortint64int64
package main
import "fmt"
func bubbleSort(sequence []int64) {
for i := 0; i < len(sequence)-1; i++ {
for j := 0; j < len(sequence)-1-i; j++ {
if sequence[j] > sequence[j+1] {
sequence[j], sequence[j+1] = sequence[j+1], sequence[j]
}
}
}
}
func main() {
var sequence = []int64{12, 11, 100, 99, 88, 23}
bubbleSort(sequence)
fmt.Println(sequence)
}
有时候在开发的时候,为了统一使用一个函数处理,没有泛型情况下,需要对类型进行断言处理,例如在某种情况下居然写出下面这样的代码:
func bubbleSortByInterface(sequence []interface{}) {
switch sequence[0].(type) {
case int64:
var arr []int64
for _, val := range sequence {
arr = append(arr, val.(int64))
}
for i := 0; i < len(arr)-1; i++ {
for j := 0; j < len(arr)-1-i; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
fmt.Println(arr)
case float64:
var arr []float64
for _, val := range sequence {
arr = append(arr, val.(float64))
}
for i := 0; i < len(arr)-1; i++ {
for j := 0; j < len(arr)-1-i; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
fmt.Println(arr)
default:
panic("type not support!!!")
}
}
interface:
func main() {
var sequence = []interface{}{12.12, 1.1, 99.4, 99.2, 88.8, 2.3}
bubbleSortByInterface(sequence)
// [1.1 2.3 12.12 88.8 99.2 99.4]
}
泛型版本
Go GenericGo
// GenericFunc 一个标准的泛型函数模板
func GenericFunc[T any](args T) {
// logic code
}
[T any]GoTany

跑起来这个泛型函数,可以正常运行,但是别急,我们写一个泛型加法函数试试:

Invalid operation: a + b (the operator + is not defined on []T)TJavaGo
Go类型集合
type MyInt int8
// 注意看~int8
func add[T int64 | float64 | ~int8](a, b T) T {
return a + b
}
func main() {
// 限制底层数据类型
fmt.Println("MyInt 1 + 2 = ",add[MyInt](1,2))
}
type xx int8~
func bubbleSortByGeneric[T int64 | float64](sequence []T) {
for i := 0; i < len(sequence)-1; i++ {
for j := 0; j < len(sequence)-1-i; j++ {
if sequence[j] > sequence[j+1] {
sequence[j], sequence[j+1] = sequence[j+1], sequence[j]
}
}
}
}
Go
func bubbleSortByGeneric[T int64 | float64](sequence []T) {
for i := 0; i < len(sequence)-1; i++ {
for j := 0; j < len(sequence)-1-i; j++ {
if sequence[j] > sequence[j+1] {
sequence[j], sequence[j+1] = sequence[j+1], sequence[j]
}
}
}
}
其实这个排序算法已经改写成了泛型版本,并且约束了参数的数据类型也可以认为类型的行为特征约束:
func main() {
var sequence1 = []int64{100, 23, 14, 66, 78, 12, 8}
bubbleSortByGeneric(sequence1)
fmt.Println(sequence1)
var sequence2 = []float64{120.13, 2.3, 112.3, 66.5, 78.12, 1.2, 8}
bubbleSortByGeneric(sequence2)
fmt.Println(sequence2)
}
结果为下图:

stackvalue
type Element interface {
int64 | float64 | string
}
type Stack[V Element] struct {
size int
value []V
}
valueElementStackStack[V Element]
Stacks *Stack[V]Stack
func (s *Stack[V]) Push(v V) {
s.value = append(s.value, v)
s.size++
}
func (s *Stack[V]) Pop() V {
e := s.value[s.size-1]
if s.size != 0 {
s.value = s.value[:s.size-1]
s.size--
}
return e
}
使用就和函数泛型差不多,在中括号里面指定泛型类型:
func main() {
// INT STACK
strS := Stack[int64]{}
strS.Push(1)
strS.Push(2)
strS.Push(3)
fmt.Println(strS.Pop())
fmt.Println(strS.Pop())
fmt.Println(strS.Pop())
// FLOAT STACK
floatS := Stack[float64]{}
floatS.Push(1.1)
floatS.Push(2.2)
floatS.Push(3.3)
fmt.Println(floatS.Pop())
fmt.Println(floatS.Pop())
fmt.Println(floatS.Pop())
}

Javacomparable
func SumNumbers[K comparable, V Number](m map[K]V) V {
var s V
for _, v := range m {
s += v
}
return s
}
func whoisMin[T Number](a, b T) T {
if a < b {
return a
}
return b
}
func main() {
// Initialize a map for the integer values
ints := map[string]int64{
"first": 34,
"second": 12,
}
// Initialize a map for the float values
floats := map[int8]float64{
-128: 35.98,
127: 26.99,
}
fmt.Printf("Generic Sums with Constraint: %v and %v\n",
SumNumbers(ints),
SumNumbers(floats))
fmt.Println(whoisMin[int64](100, 1000))
}
comparablemapKeycomparableGO
comparableinterfaceGocomparable[T]mapmapmap
本文章实例代码仓库: