Go 泛型 in 1.25

这篇没啥好讲的,个人学习笔记,纯引个流。

go 复制代码
package main

import (
	"fmt"
)

// TIP To run your code, right-click the code and select <b>Run</b>. Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.

type MyStruct interface {
	~int | ~float32 // 支持所有底层类型是 int 或 float32(例如 MyInt,MyFloat)
}

func PrintValue[T MyStruct](v T) {
	fmt.Println(v)
}

type MyInt int
type MyFloat float32

func PrintValue2[T int | float32](v T) {
	fmt.Println(v)
}

/*
	⚠️ 为什么 Go 要区分 T 和 ~T?

Go 的类型系统强调 "命名类型(named type)" vs "底层类型(underlying type)":

type MyInt int 是一个新类型,与 int 不等价(不能直接赋值给 int 变量,除非显式转换);
但在泛型中,你可能希望允许所有底层是整数的类型参与运算(比如写一个通用的 Add 函数);
~T 就是为了这种场景设计的 ------ "只要底层是 T,我就接受"。
*/
type CompareStruct[T comparable] struct {
	num1 T
	num2 T
}

/*
Go 1.22+ 内建了常用约束(无需 import):
comparable:支持 ==/!=
ordered:支持 <, >, <=, >=(即 int, float64, string 等)
*/

type Box[T any] struct{ value T }

// GetValue ✅ 泛型类型可以有普通方法d
func (b Box[T]) GetValue() T { return b.value }

/*
Go 1.25 移除了核心类型的设定
根据 Go 1.24 的规范,对类型参数为P T([T ~[]byte | string]) 的变量进行切片操作 (s[i:j]) 是不允许的,
因为 T 没有核心类型(具有多个底层类型),即使切片操作对[]byte和string本身都是合法的。
*/
func main() {
	// instance 1
	PrintValue(MyInt(32))
	PrintValue(MyFloat(3.14))

	// instance 2
	PrintValue2(32)
	PrintValue2(float32(3.14))
	//PrintValue2(MyInt(32)) 不支持
	//PrintValue2(MyFloat(3.14)) 不支持

	// instance 3
	st1 := CompareStruct[int]{
		num1: 2,
		num2: 3,
	}
	st2 := CompareStruct[int]{
		num1: 3,
		num2: 5,
	}
	if st1.num2 > st2.num2 {
		fmt.Println("true")
	} else {
		fmt.Println("false")
	}

	// instance 4
	box := Box[int]{
		10000,
	}
	PrintValue(box.GetValue())
}
相关推荐
橘颂TA1 天前
线程池与线程安全:后端开发的 “性能 + 安全” 双维实践
java·开发语言·安全
bruce_哈哈哈1 天前
go语言初认识
开发语言·后端·golang
十五年专注C++开发1 天前
VS2019编译的C++程序,在win10正常运行,在win7上Debug正常运行,Release运行报错0xC0000005,进不了main函数
开发语言·c++·报错c0x0000005
一条咸鱼_SaltyFish1 天前
[Day13] 微服务架构下的共享基础库设计:contract-common 模块实践
开发语言·人工智能·微服务·云原生·架构·ai编程
隐退山林1 天前
JavaEE:多线程初阶(一)
java·开发语言·jvm
C_心欲无痕1 天前
ts - 模板字面量类型与 `keyof` 的魔法组合:`keyof T & `on${string}`使用
linux·运维·开发语言·前端·ubuntu·typescript
最贪吃的虎1 天前
Redis其实并不是线程安全的
java·开发语言·数据库·redis·后端·缓存·lua
乾元1 天前
无线定位与链路质量预测——从“知道你在哪”,到“提前知道你会不会掉线”的网络服务化实践
运维·开发语言·人工智能·网络协议·重构·信息与通信
AC赳赳老秦1 天前
Unity游戏开发实战指南:核心逻辑与场景构建详解
开发语言·spring boot·爬虫·搜索引擎·全文检索·lucene·deepseek