1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import (
14 "fmt"
15 )
16
17 type C comparable
18
19 type value[T C] struct {
20 val T
21 }
22
23 func (v *value[T]) test(def T) bool {
24 return (v.val == def)
25 }
26
27 func (v *value[T]) get(def T) T {
28 var c value[int]
29 if c.test(32) {
30 return def
31 } else if v.test(def) {
32 return def
33 } else {
34 return v.val
35 }
36 }
37
38 func main() {
39 var s value[string]
40 if got, want := s.get("ab"), ""; got != want {
41 panic(fmt.Sprintf("get() == %d, want %d", got, want))
42 }
43 }
44
View as plain text