1
2
3
4
5
6
7 package main
8
9 type I interface {
10 foo() int
11 }
12
13
14
15 func f[T I](x T) int {
16 return x.foo()
17 }
18
19 type squarer int
20
21 func (x squarer) foo() int {
22 return int(x*x)
23 }
24
25 type doubler int
26
27 func (x doubler) foo() int {
28 return int(2*x)
29 }
30
31 type incrementer int16
32
33 func (x *incrementer) foo() int {
34 return int(*x+1)
35 }
36
37 type decrementer int32
38
39 func (x *decrementer) foo() int{
40 return int(*x-1)
41 }
42
43 func main() {
44 println(f(squarer(5)))
45 println(f(doubler(5)))
46 var i incrementer = 5
47 println(f(&i))
48 var d decrementer = 5
49 println(f(&d))
50 }
51
View as plain text