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

View as plain text