ITPub博客

首页 > 应用开发 > Go语言 > golang复习-golang泛型测试

golang复习-golang泛型测试

原创 Go语言 作者:风尘_NULL 时间:2025-10-02 16:47:34 0 删除 编辑

package main

 

import "fmt"

 

type Mytest struct {

A int

}

type Mytest1 struct {

B string

}

type Dataer interface {

Mytest1 | Mytest

}

type Inner interface {

string | int

}

type Mytester [ T Inner ] interface {

Dataer

Generic () T

}

type TrueInterface interface {

D

}

 

func ( mytest Mytest ) Generic () int {

return mytest . A

}

func ( mytest Mytest1 ) Generic () string {

return mytest . B

}

 

//T Mytester[Inner]不行,但是可以T Mytester[int],这个泛型其实有点鸡肋

func DoWork [ T Mytester [ int ]]( things [] T ) {

for _ , v := range things {

 

fmt . Printf ( "%v" , v . Generic ())

}

}

func main () {

c := Mytest { 5 }

DoWork ([] Mytest { c })

}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30221425/viewspace-3097005/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
腾讯科技(深圳)有限公司 DBA

注册时间:2015-04-24

  • 博文量
    76
  • 访问量
    238210