Skip to content

Commit 4d27c11

Browse files
Merge pull request #3 from chenmingyong0423/feature/generic
支持泛型参数
2 parents 68442bf + 541c029 commit 4d27c11

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

example/generic_example.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
package example
1616

17-
type Example[T any] struct {
17+
type GenericExample[T any, U comparable, V ~int] struct {
1818
A T `opt:"-"`
19-
B T
20-
C int
19+
B U
20+
C V
21+
D string
2122
}

example/opt_example_gen.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

example/opt_generic_example_gen.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Generated by optioner -type GenericExample; DO NOT EDIT
2+
// If you have any questions, please create issues and submit contributions at:
3+
// https://github.com/chenmingyong0423/go-optioner
4+
5+
package example
6+
7+
type GenericExampleOption[T any, U comparable, V ~int] func(*GenericExample[T, U, V])
8+
9+
func NewGenericExample[T any, U comparable, V ~int](a T, opts ...GenericExampleOption[T, U, V]) *GenericExample[T, U, V] {
10+
genericExample := &GenericExample[T, U, V]{
11+
A: a,
12+
}
13+
14+
for _, opt := range opts {
15+
opt(genericExample)
16+
}
17+
18+
return genericExample
19+
}
20+
21+
func WithB[T any, U comparable, V ~int](b U) GenericExampleOption[T, U, V] {
22+
return func(genericExample *GenericExample[T, U, V]) {
23+
genericExample.B = b
24+
}
25+
}
26+
27+
func WithC[T any, U comparable, V ~int](c V) GenericExampleOption[T, U, V] {
28+
return func(genericExample *GenericExample[T, U, V]) {
29+
genericExample.C = c
30+
}
31+
}
32+
33+
func WithD[T any, U comparable, V ~int](d string) GenericExampleOption[T, U, V] {
34+
return func(genericExample *GenericExample[T, U, V]) {
35+
genericExample.D = d
36+
}
37+
}

0 commit comments

Comments
 (0)