golang复习-golang泛型测试

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 })

}


请使用浏览器的分享功能分享到微信等