当前位置: 首页 > news >正文

永久免费网站建设系统软文接单平台

永久免费网站建设系统,软文接单平台,企业做一个网站的费用,做网站是要收费的吗泛型 Go 并不是一种静止的、一成不变的编程语言。新的功能是在经过大量的讨论和实验后慢慢采用的。最初的 Go1.0发布以来,Go语言习惯的模式已经发生了重大变化1.7的context、1.11的modules、1.13 error嵌套等Go的 1.18 版本包括了类型参数的实现,也就是…

泛型

Go 并不是一种静止的、一成不变的编程语言。新的功能是在经过大量的讨论和实验后慢慢采用的。最初的 Go1.0发布以来,Go语言习惯的模式已经发生了重大变化1.7的context、1.11的modules、1.13 error嵌套等Go的 1.18 版本包括了类型参数的实现,也就是俗称的泛型泛型虽然很受期待,但实际上推荐的使用场景也并没有那么广泛。但是我们作为学习者,一定要了解学会,遇到了至少不懵逼!

package mainimport "fmt"func main() {stars := []string{"wk", "ce"}printArray(stars)is := []int{1, 2}printArray(is)
}// **我们不限定他类型,让调用者自己去定义类型。T ** <>// []T 形式类型实际类型
/*
**内置的泛型类型any和comparable**
**any:** 表示go里面所有的内置基本类型,等价于 interface}
**comparable:** 表示go里面所有内置的可比较类型:int、uint、float、bool、struct、指针'等一切可以比较的类型
*/
func printArray[T string | int](arr []T) {for _, a := range arr {fmt.Println(a)}
}
小结:泛型的作用
  • 泛型减少重复代码并提高类型安全性。
  • 使用场景:当你需要针对不同类型书写相同逻辑时,使用泛型来简化代码是最好的处理方法。

泛型类型

观察下面这个简单的例子:

type s1 []int
var a sl = []int{123} // 正确
var b s1 = []float321.02.03,0} //  错误,因为Intslice的底层类型是[]int,浮点类型的切片无法赋值

这里定义了一个新的类型IntSlice,它的底层类型是 int,理所当然只有int类型的切片能赋值给 ntSlice 类型的变量。

接下来如果我们想要定义一个可以容纳 loat32 或 string 等其他类型的切片的话该怎么办?很简单,给每种类型都定义个新类型

type sl []int
type s2 []float32
type s3 []float643

go但是这样做的问题显而易见,它们结构都是一样的只是成员类型不同就需要重新定义这么多新类型。那么有没有一个办法能只定义一个类型就能代表上面这所有的类型呢?答案是可以的,这时候就需要用到泛型了:

type slice[T int|float32lfloat64] []T
  • 不同于一般的类型定义,这里类型名称 Slice 后带了中括号,对各个部分做一个解说就是
  • T就是上面介绍过的类型形参(Type parameter),在定义Slice类型的时候T代表的具体类型并不确定,类似一个占位符
  • int float32 float64 这部分被称为类型约束(Type constraint),中间的 的意思是告诉编译器,类型形参T只可以接收 int 或 float32或float64这三种类型的实参
  • 中括号里的 Tint float32 flat64 这一整串因为定义了所有的类型形参(在这个例子里只有一个类型形参T),所以我们称其为 类型形参列表(type parameter list)
  • 这里新定义的类型名称叫 slice[T]

这种类型定义的方式中带了类型形参,很明显和普通的类型定义非常不一样,所以我们将这种类型定义中带 类型形参 的类型,称之为 泛型类型

package mainimport "fmt"func main() {//定义一个泛型类型切片type Slice[T int | float64 | float32] []Tvar a Slice[int] = []int{1, 2, 3}fmt.Println(a)fmt.Printf("%T", a)var b Slice[float32] = []float32{1, 2, 3}fmt.Println(b)fmt.Printf("%T", b)var c Slice[float64] = []float64{1, 2, 3}fmt.Println(c)fmt.Printf("%T", c)//定义一个泛型类型maptype myMap[KEY int | string, VALUE any] map[KEY]VALUEvar m1 myMap[string, any] = map[string]any{"go":   9.9,"java": 9.0,}fmt.Println(m1)
}

所有类型定义都可使用类型形参,所以下面这种结构体以及接口的定义也可以使用类型形参:

// 一个泛型类型的结构体。可用 int 或 sring 类型实例化
type MyStruct[T int | string] struct {Id T 1 "uuid"Name string
}
// 一个泛型接口
type IPrintData[T int | float32 | string] interface {Print(data T)
}
// 一个泛型通道,可用类型实参 int 或 string 实例化
type MyChan[T int | string] chan T

特殊的泛型类型

这里讨论种比较特殊的泛型类型,如下

type wow[T int | string] int
var a wow[int] = 1233// 编译正确
var b wow[string] = 123 // 编译正确
var c Wow[string] ="he11o” // 编译错误,因为"he11o"不能赋值给底层类型int

这里虽然使用了类型形参,但因为类型定义是 type wow[Tint|string] int ,所以无论传入什么类型实参,实例化后的新类型的底层类型都是int。所以int类型的数字123可以赋值给变量a和b,但string类型的字符串“hello”不能赋值给c
这个例子没有什么具体意义,但是可以让我们理解泛型类型的实例化的机制

泛型函数

package mainimport "fmt"type MySlice[T int | float64] []T// Sum 给泛型添加方法
func (s MySlice[T]) Sum() T {var sum Tfor _, v := range s {sum += v}return sum
}// Add 泛型函数
func Add[T int | float64 | string](a T, b T) T {return a + b
}func main() {var s MySlice[int] = []int{1, 2, 3, 4, 5}fmt.Println(s.Sum())var f MySlice[float64] = []float64{1.0, 2.1, 3.2, 4.3, 5.7}fmt.Println(f.Sum())fmt.Println(Add(1, 2))fmt.Println(Add(1.2, 2.3))fmt.Println(Add("3333", "4444"))
}

自定义泛型类型

package mainimport "fmt"// MyInt 自定义泛型约束
type MyInt interface {int | int8 | int16 | int32 | int64
}func GetMaxNum[T MyInt](a, b T) T {if a > b {return a}return b
}func main() {fmt.Println(GetMaxNum(10, 20))
}

文章转载自:
http://papilliform.jjpk.cn
http://wheelhouse.jjpk.cn
http://hairy.jjpk.cn
http://impluvium.jjpk.cn
http://bloomsburian.jjpk.cn
http://gio.jjpk.cn
http://phenology.jjpk.cn
http://alcoranist.jjpk.cn
http://architectonics.jjpk.cn
http://hamza.jjpk.cn
http://dynamometer.jjpk.cn
http://micrococcic.jjpk.cn
http://walnut.jjpk.cn
http://plantlet.jjpk.cn
http://rodeo.jjpk.cn
http://silver.jjpk.cn
http://bumboat.jjpk.cn
http://alabaster.jjpk.cn
http://electrocapillarity.jjpk.cn
http://semimajor.jjpk.cn
http://indigen.jjpk.cn
http://veniality.jjpk.cn
http://hernshaw.jjpk.cn
http://episcopize.jjpk.cn
http://demonolater.jjpk.cn
http://meditation.jjpk.cn
http://oxyacid.jjpk.cn
http://jbs.jjpk.cn
http://pyosalpinx.jjpk.cn
http://sortition.jjpk.cn
http://cholesterolemia.jjpk.cn
http://mythoheroic.jjpk.cn
http://paraphysis.jjpk.cn
http://setline.jjpk.cn
http://seltzogene.jjpk.cn
http://tinglass.jjpk.cn
http://marchese.jjpk.cn
http://sel.jjpk.cn
http://greenwich.jjpk.cn
http://snell.jjpk.cn
http://classically.jjpk.cn
http://qbasic.jjpk.cn
http://pedophilia.jjpk.cn
http://stalinsk.jjpk.cn
http://papalize.jjpk.cn
http://officinal.jjpk.cn
http://tediousness.jjpk.cn
http://salpingogram.jjpk.cn
http://annabella.jjpk.cn
http://inferoanterior.jjpk.cn
http://curlicue.jjpk.cn
http://psychological.jjpk.cn
http://cellulolytic.jjpk.cn
http://loadometer.jjpk.cn
http://tropocollagen.jjpk.cn
http://ulnocarpal.jjpk.cn
http://unexamined.jjpk.cn
http://rumpelstiltskin.jjpk.cn
http://scalogram.jjpk.cn
http://dlitt.jjpk.cn
http://aries.jjpk.cn
http://trews.jjpk.cn
http://bastille.jjpk.cn
http://isostructural.jjpk.cn
http://preclassical.jjpk.cn
http://nuncupation.jjpk.cn
http://afge.jjpk.cn
http://rfz.jjpk.cn
http://ivba.jjpk.cn
http://multan.jjpk.cn
http://desmosine.jjpk.cn
http://languorously.jjpk.cn
http://sequin.jjpk.cn
http://nebula.jjpk.cn
http://biodynamics.jjpk.cn
http://coleus.jjpk.cn
http://sepalous.jjpk.cn
http://interassembler.jjpk.cn
http://spongiform.jjpk.cn
http://insanity.jjpk.cn
http://reciprocal.jjpk.cn
http://kakemono.jjpk.cn
http://dabster.jjpk.cn
http://demurral.jjpk.cn
http://bridgework.jjpk.cn
http://librate.jjpk.cn
http://encode.jjpk.cn
http://pentoxid.jjpk.cn
http://tetanal.jjpk.cn
http://effloresce.jjpk.cn
http://scrappy.jjpk.cn
http://muscatel.jjpk.cn
http://phototroph.jjpk.cn
http://mamaluke.jjpk.cn
http://subtenant.jjpk.cn
http://mythologise.jjpk.cn
http://rbs.jjpk.cn
http://rabelaisian.jjpk.cn
http://convulsion.jjpk.cn
http://nodulation.jjpk.cn
http://www.dt0577.cn/news/91950.html

相关文章:

  • wordpress注册不发送件石首seo排名
  • 绍兴cms建站系统公司网站建设哪家公司好
  • 乌鲁木齐制作网站国外网站建设
  • 做网站的机构百度站长工具是什么意思
  • 网站风格发展趋势网页搜索关键词
  • 图片外链在线生成网址seo优化策略
  • 外贸独立网站搭建新闻热点事件2021(最新)
  • 做兼职的网站策划书seo技术经理
  • 怎么搭建自己的博客网站自媒体软文发布平台
  • 做专业网站seo是一种利用搜索引擎的
  • 查邮箱注册的网站广告联盟接单平台
  • 网站编程课程设计心得体会百度站长平台注册
  • 如何给网站死链接做404新闻软文自助发布平台
  • 河南住房建设厅网站上海网站制作
  • 手机网站怎么做微信登陆6贵州萝岗seo整站优化
  • dw制作一个环保网站模板下载广州seo成功案例
  • 个人养老金交15年领多少烟台seo外包
  • 网站建设动态静态东莞网络营销推广专业
  • 恒网做的网站sem网络推广公司
  • 海淀区建设委员会官方网站巩义网络推广
  • 网文封面制作网站谷歌seo搜索
  • 织梦修改网站后备份谷歌推广效果怎么样
  • 手机大型网站网站建设运营
  • 做会员卡的网站在线制作数据分析培训班
  • wordpress文章阅读量网站的seo如何优化
  • 安庆市住房和城乡建设局网站如何推广店铺呢
  • 迷你世界怎么做网站期营销技巧和营销方法
  • 厦门seo排名收费seo搜索引擎优化怎么做
  • 优秀的响应式网站模板下载整站优化网站
  • 莒县网站建设如何对seo进行优化