Source file test/typeparam/typeswitch2.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 "fmt"
    10  
    11  func f[T any](i interface{}) {
    12  	switch x := i.(type) {
    13  	case T:
    14  		fmt.Println("T", x)
    15  	case int:
    16  		fmt.Println("int", x)
    17  	case int32, int16:
    18  		fmt.Println("int32/int16", x)
    19  	case struct{ a, b T }:
    20  		fmt.Println("struct{T,T}", x.a, x.b)
    21  	default:
    22  		fmt.Println("other", x)
    23  	}
    24  }
    25  func main() {
    26  	f[float64](float64(6))
    27  	f[float64](int(7))
    28  	f[float64](int32(8))
    29  	f[float64](struct{ a, b float64 }{a: 1, b: 2})
    30  	f[float64](int8(9))
    31  	f[int32](int32(7))
    32  	f[int](int32(7))
    33  	f[any](int(10))
    34  	f[interface{ M() }](int(11))
    35  }
    36  

View as plain text