Source file src/go/doc/testdata/examples/generic_constraints.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package p_test
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  )
    11  
    12  type C1 interface {
    13  	string | int
    14  }
    15  
    16  type C2 interface {
    17  	M(time.Time)
    18  }
    19  
    20  type G[T C1] int
    21  
    22  func g[T C2](x T) {}
    23  
    24  type Tm int
    25  
    26  func (Tm) M(time.Time) {}
    27  
    28  type Foo int
    29  
    30  func Example() {
    31  	fmt.Println("hello")
    32  }
    33  
    34  func ExampleGeneric() {
    35  	var x G[string]
    36  	g(Tm(3))
    37  	fmt.Println(x)
    38  }
    39  

View as plain text