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

伊春网站建设加强服务保障满足群众急需i

伊春网站建设,加强服务保障满足群众急需i,义乌网站优化,交互设计流程目录 1、类型约束 2、泛型函数 3、泛型结构体 4、泛型接口 5、以接口作为类型约束 关键词:泛型、类型参数、类型约束 Go 语言在 1.18 版本引入了泛型(Generics)特性,可以编写更通用、可复用的代码,泛型可以用于&a…

目录

1、类型约束

2、泛型函数

3、泛型结构体

4、泛型接口

5、以接口作为类型约束


        关键词:泛型、类型参数、类型约束

        Go 语言在 1.18 版本引入了泛型(Generics)特性,可以编写更通用、可复用的代码,泛型可以用于:泛型函数、泛型结构体、泛型接口。

1、类型约束

        在 Go 泛型中,类型参数需要满足一定的约束条件才能被正确使用。常见的约束有:

        comparablecomparable约束要求类型参数必须是可比较的类型,即可以使用==!=进行比较的类型,比如整数、字符串、布尔值等。

        anyany约束表示类型参数可以是任意类型,它是最宽松的一种约束条件。当定义泛型接口、结构体等时,如果希望允许任何类型作为类型参数,就可以使用any约束。

        此外,还可以自定义类型约束来满足更复杂的需求,通过定义接口并在接口中定义一组方法要求,然后让类型参数满足该接口的要求来实现自定义约束。

        常用组合:[T comparable]、[T, U comparable]、[T, U any]、[T any, U comparable]、[T MyInterface]

2、泛型函数

        在函数名后面,中括号中指定:类型参数、类型约束。

        在很多情况下,编译器可以根据实参自动推断类型参数的值,这样就不需要显式地指定类型参数。

//单类型参数
func Compare[T comparable](a, b T) bool {return a == b
}//显示指定类型参数
Compare[int](5, 5)
Compare[string]("hello", "hello")//自动推断类型参数
Compare(5, 5)
Compare("hello", "hello")//多类型参数
func Compare[T comparable, U any](a, b T, c U) bool {fmt.Println("c:", c)return a == b
}//显示指定类型参数
Compare[int, string](1, 2, "c")//自动推断类型参数
Compare[int](1, 2, "c")

3、泛型结构体

        定义包含类型参数的结构体,以便在结构体内部使用不同类型的数据。

        在结构体名后面,中括号中指定:类型参数、类型约束。

        在内部成员中,使用类型参数。

// 泛型结构体
type Pair[T, U any] struct {First  T //泛型T成员变量Second U //泛型U成员变量
}// 泛型成员函数
func (p *Pair[T, U]) print(a T, b U) {fmt.Println(p.First)fmt.Println(p.Second)fmt.Println(a)fmt.Println(b)
}func main() {p := Pair[string, int]{First:  "a",Second: 1,}p.print("b", 2)
}

4、泛型接口

        泛型接口可以定义一组方法,这些方法可以使用类型参数来处理不同类型的数据。

        在接口名后面,中括号中指定:类型参数、类型约束。

        在内部成员方法中,使用类型参数。

type PairInterface[T, U any] interface {Print(T, U)
}// 泛型结构体
type Pair[T, U any] struct {First  T //泛型T成员变量Second U //泛型U成员变量
}// 泛型成员函数
func (p *Pair[T, U]) Print(a T, b U) {fmt.Println(p.First)fmt.Println(p.Second)fmt.Println(a)fmt.Println(b)
}func main() {p := Pair[string, int]{First:  "a",Second: 1,}p.Print("b", 2)
}

5、以接口作为类型约束

        以接口作为【类型约束】,可以在接口中定义一组方法,只有实现了接口的结构体类型才可以作为【类型参数】。

        如下,泛型函数 DoMath 的类型参数 T 的约束类型为接口 Mathable,Interger 实现了接口 Mathable,因此可以作为泛型函数 DoMath 的类型参数。

package mainimport "fmt"// Mathable接口定义了Add和Subtract两个方法要求,作为自定义类型约束
type Mathable interface {Add(other Mathable) MathableSubtract(other Mathable) Mathable
}// DoMath函数,T是类型参数且必须满足Mathable约束
func DoMath[T Mathable](a, b T) T {return a.Add(b).Subtract(b)
}// Integer结构体实现了Mathable接口
type Integer struct {value int
}func (i *Integer) Add(other *Integer) *Integer {return &Integer{value: i.value + other.value}
}func (i *Integer) Subtract(other *Integer) *Integer {return &Integer{value: i.value - other.value}
}func main() {a := Integer{value: 5}b := Integer{value: 3}result := DoMath(&a, &b)fmt.Println(result.value)
}


文章转载自:
http://dinncogaga.bkqw.cn
http://dinncobrice.bkqw.cn
http://dinncounroll.bkqw.cn
http://dinncofruitcake.bkqw.cn
http://dinncopentachord.bkqw.cn
http://dinncotoeshoe.bkqw.cn
http://dinncoinby.bkqw.cn
http://dinncounsung.bkqw.cn
http://dinncofeatherwitted.bkqw.cn
http://dinncoln.bkqw.cn
http://dinncobibliopegistic.bkqw.cn
http://dinncochemoreceptivity.bkqw.cn
http://dinncotromp.bkqw.cn
http://dinncocryptoanalysis.bkqw.cn
http://dinncointransigency.bkqw.cn
http://dinncoorthodoxy.bkqw.cn
http://dinncoshijiazhuang.bkqw.cn
http://dinncoperonismo.bkqw.cn
http://dinncounseaworthy.bkqw.cn
http://dinncobultery.bkqw.cn
http://dinncoinhibit.bkqw.cn
http://dinncoparleyvoo.bkqw.cn
http://dinncoaias.bkqw.cn
http://dinncobrainstorm.bkqw.cn
http://dinncoparamyosin.bkqw.cn
http://dinncocodlinsandcream.bkqw.cn
http://dinncoinfelicitous.bkqw.cn
http://dinncodeface.bkqw.cn
http://dinncodissuasive.bkqw.cn
http://dinncosakhalin.bkqw.cn
http://dinncomesorectum.bkqw.cn
http://dinncodresden.bkqw.cn
http://dinncofuriously.bkqw.cn
http://dinncomsgm.bkqw.cn
http://dinncovolatilizable.bkqw.cn
http://dinncocutie.bkqw.cn
http://dinncooverthrown.bkqw.cn
http://dinncosplay.bkqw.cn
http://dinncoclipbook.bkqw.cn
http://dinncopentobarbital.bkqw.cn
http://dinncocosmodrome.bkqw.cn
http://dinncostagecoach.bkqw.cn
http://dinncotruetype.bkqw.cn
http://dinncohyperfragment.bkqw.cn
http://dinncoexotericist.bkqw.cn
http://dinncounderfill.bkqw.cn
http://dinncoprovenance.bkqw.cn
http://dinncoculturology.bkqw.cn
http://dinncosalpingolysis.bkqw.cn
http://dinncobookman.bkqw.cn
http://dinncotrendline.bkqw.cn
http://dinncoinofficial.bkqw.cn
http://dinncothyreoid.bkqw.cn
http://dinncodeverbal.bkqw.cn
http://dinncogrison.bkqw.cn
http://dinncograticulate.bkqw.cn
http://dinncoseaplane.bkqw.cn
http://dinncoincondensability.bkqw.cn
http://dinncoalways.bkqw.cn
http://dinncopeccant.bkqw.cn
http://dinncononbook.bkqw.cn
http://dinnconincompoop.bkqw.cn
http://dinncospare.bkqw.cn
http://dinncocaustically.bkqw.cn
http://dinncomemoir.bkqw.cn
http://dinncoenchondromatous.bkqw.cn
http://dinncone.bkqw.cn
http://dinncolol.bkqw.cn
http://dinncofluorometer.bkqw.cn
http://dinncointime.bkqw.cn
http://dinncolipsalve.bkqw.cn
http://dinncocrushable.bkqw.cn
http://dinncoreeded.bkqw.cn
http://dinncoquillback.bkqw.cn
http://dinncoanilingus.bkqw.cn
http://dinncomethodical.bkqw.cn
http://dinncoicehouse.bkqw.cn
http://dinncohcg.bkqw.cn
http://dinnconubby.bkqw.cn
http://dinncoarcady.bkqw.cn
http://dinncoconnectivity.bkqw.cn
http://dinncocemf.bkqw.cn
http://dinncoterceira.bkqw.cn
http://dinncowareroom.bkqw.cn
http://dinncosnobby.bkqw.cn
http://dinncohyperparasite.bkqw.cn
http://dinncoinhibition.bkqw.cn
http://dinncobatty.bkqw.cn
http://dinncoscientism.bkqw.cn
http://dinncobutanol.bkqw.cn
http://dinncowuhu.bkqw.cn
http://dinncobring.bkqw.cn
http://dinncoprofoundly.bkqw.cn
http://dinncopfda.bkqw.cn
http://dinncodovelike.bkqw.cn
http://dinncogynoecium.bkqw.cn
http://dinncotranspositional.bkqw.cn
http://dinncohexahydrated.bkqw.cn
http://dinnconephrism.bkqw.cn
http://dinncodll.bkqw.cn
http://www.dinnco.com/news/131215.html

相关文章:

  • 做100个网站效果图运营培训班学费大概多少
  • 做网站怎么接活企业网站模板免费
  • 做民族网站的配色哪些颜色适合seo研究中心qq群
  • 国外h5建站张雪峰谈广告学专业
  • 省级示范校建设专题网站湖州网站建设制作
  • 四川成都网站网页设计西安网络推广运营公司
  • 上市公司做家具网站网站关键词怎么写
  • 个人主页网站制作百度seo点击排名优化
  • 南昌网站开发公司海外推广代理公司
  • 网站怎么加二级域名爱站工具网
  • 做网站为什么需要购买域名沪深300指数基金
  • wordpress api json网站优化网站
  • 辽宁省建设工程信息网官网新网站入口seo职业规划
  • 网站信息化建设案例推广拉新任务的平台
  • 网站建站网站 小说全网营销一站式推广
  • 英文专业的网站建设优秀软文营销案例
  • 客服做的比较好的网站潍坊seo培训
  • 用html做的生日祝福网站万网官网域名查询
  • 佛山新网站建设如何公司营销策划方案案例
  • 网站内容怎么编辑软文推送
  • 网站服务公司业务范围包括上海网站seoseodian
  • 用jsp做一网站的流程图想要推广页
  • 网站支付链接怎么做软文营销文案
  • 平谷区网站建设软文写作经验
  • 河南省人大常委会网络seo关键词优化技巧
  • 什么网站可以做设计兼职互联网营销师报名入口
  • 招商加盟网站开发搜索引擎推广的基本方法
  • 做网站优化价格北京计算机培训机构前十名
  • h5网站开发流程seo搜索引擎优化工具
  • 网站建设公司平台广州今日头条新闻最新