]> Cypherpunks.ru repositories - gostls13.git/blob - test/typeparam/stringable.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / typeparam / stringable.go
1 // run
2
3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
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 // StringableList is a slice of some type, where the type
20 // must have a String method.
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 }