Ai
1 Star 0 Fork 0

清风/博客代码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 760 Bytes
一键复制 编辑 原始数据 按行查看 历史
lidiqing 提交于 2024-07-12 19:58 +08:00 . 增加demo
package main
import (
"fmt"
"golang.org/x/exp/constraints"
"reflect"
)
type PointX int32
type Point []int32
func main() {
var pX PointX
pX = 10
fmt.Println("pX的类型是:", reflect.TypeOf(pX)) //pX的类型是: main.PointX
Scale(pX) //等价于Scale[PointX](pX)
p := Point{2, 1, 3}
fmt.Println("p的类型是:", reflect.TypeOf(p)) //p的类型是: main.Point
Scale2(p) //等价于Scale2[Point](p)
}
func Scale[E constraints.Integer](s E) {
fmt.Println("Scale s的类型是:", reflect.TypeOf(s)) //Scale s的类型是: main.PointX
}
func Scale2[S ~[]E, E constraints.Integer](s S) {
fmt.Println("Scale2 s的类型是:", reflect.TypeOf(s)) //Scale2 s的类型是: main.Point
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qingfeng-169/blog-code.git
git@gitee.com:qingfeng-169/blog-code.git
qingfeng-169
blog-code
博客代码
master

搜索帮助