1
2
3
4
5
6
7
8
9 package main
10
11 import "fmt"
12
13 type A[T any] struct {
14 B[T]
15 }
16
17 type B[T any] struct {
18 val T
19 }
20
21 func (b *B[T]) get() T {
22 return b.val
23 }
24
25 type getter[T any] interface {
26 get() T
27 }
28
29
30 func doGet[T any](i getter[T]) T {
31 return i.get()
32 }
33
34
35 func doGet2[T any](i interface{}) T {
36 i2 := i.(getter[T])
37 return i2.get()
38 }
39
40 func main() {
41 a := A[int]{B: B[int]{3}}
42 var i getter[int] = &a
43
44 if got, want := doGet(i), 3; got != want {
45 panic(fmt.Sprintf("got %v, want %v", got, want))
46 }
47
48 as := A[string]{B: B[string]{"abc"}}
49 if got, want := doGet2[string](&as), "abc"; got != want {
50 panic(fmt.Sprintf("got %v, want %v", got, want))
51 }
52 }
53
View as plain text