Source file
test/typeparam/stringable.go
1
2
3
4
5
6
7 package main
8
9 import (
10 "fmt"
11 "strconv"
12 "strings"
13 )
14
15 type Stringer interface {
16 String() string
17 }
18
19
20
21 type StringableList[T Stringer] []T
22
23 func (s StringableList[T]) String() string {
24 var sb strings.Builder
25 for i, v := range s {
26 if i > 0 {
27 sb.WriteString(", ")
28 }
29 sb.WriteString(v.String())
30 }
31 return sb.String()
32 }
33
34 type myint int
35
36 func (a myint) String() string {
37 return strconv.Itoa(int(a))
38 }
39
40 func main() {
41 v := StringableList[myint]{myint(1), myint(2)}
42
43 if got, want := v.String(), "1, 2"; got != want {
44 panic(fmt.Sprintf("got %s, want %s", got, want))
45 }
46 }
47
View as plain text