github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/test/execution/switch/type.go (about)

     1  // RUN: llgo -o %t %s
     2  // RUN: %t 2>&1 | FileCheck %s
     3  
     4  // CHECK: int64 123
     5  // CHECK-NEXT: default
     6  // CHECK-NEXT: uint8 or int8
     7  // CHECK-NEXT: uint8 or int8
     8  // CHECK-NEXT: N
     9  
    10  package main
    11  
    12  func test(i interface{}) {
    13  	switch x := i.(type) {
    14  	case int64:
    15  		println("int64", x)
    16  	// FIXME
    17  	//case string:
    18  	//	println("string", x)
    19  	default:
    20  		println("default")
    21  	}
    22  }
    23  
    24  type stringer interface {
    25  	String() string
    26  }
    27  
    28  func printany(i interface{}) {
    29  	switch v := i.(type) {
    30  	case nil:
    31  		print("nil", v)
    32  	case stringer:
    33  		print(v.String())
    34  	case error:
    35  		print(v.Error())
    36  	case int:
    37  		print(v)
    38  	case string:
    39  		print(v)
    40  	}
    41  }
    42  
    43  func multi(i interface{}) {
    44  	switch i.(type) {
    45  	case uint8, int8:
    46  		println("uint8 or int8")
    47  	default:
    48  		println("something else")
    49  	}
    50  }
    51  
    52  type N int
    53  
    54  func (n N) String() string { return "N" }
    55  
    56  func named() {
    57  	var x interface{} = N(123)
    58  	switch x := x.(type) {
    59  	case N:
    60  		// Test for bug: previously, type switch was
    61  		// assigning underlying type of N (int).
    62  		println(x.String())
    63  	}
    64  }
    65  
    66  func main() {
    67  	test(int64(123))
    68  	test("abc")
    69  	multi(uint8(123))
    70  	multi(int8(123))
    71  	named()
    72  }