在ingerface中使用泛型,实现链式方法

2022-09-26 23:15:01 golanggenerics

package main

import "fmt"

type Common struct {
	Name string
}

type ProjectInterface[T any] interface {
	Init() *T
	Func1() *T
}

type ProjectStruct struct {
	Name string
}

func (p *ProjectStruct) Init() *ProjectStruct {
	return p
}

func (p *ProjectStruct) Func1() *ProjectStruct {
	return p
}

func Test() {
	var project ProjectInterface[ProjectStruct] = (&ProjectStruct{Name: "I am Project"}).Init().Func1()
	fmt.Println(project)
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Powered By Valine
v1.5.2