github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/fixedbugs/bug113.go (about)

     1  // run
     2  
     3  // Copyright 2009 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  type I interface{}
    10  
    11  func foo1(i int) int     { return i }
    12  func foo2(i int32) int32 { return i }
    13  func main() {
    14  	var i I
    15  	i = 1
    16  	var v1 = i.(int)
    17  	if foo1(v1) != 1 {
    18  		panic(1)
    19  	}
    20  	var v2 = int32(i.(int))
    21  	if foo2(v2) != 1 {
    22  		panic(2)
    23  	}
    24  	
    25  	shouldPanic(p1)
    26  }
    27  
    28  func p1() {
    29  	var i I
    30  	i = 1
    31  	var v3 = i.(int32) // This type conversion should fail at runtime.
    32  	if foo2(v3) != 1 {
    33  		panic(3)
    34  	}
    35  }
    36  
    37  func shouldPanic(f func()) {
    38  	defer func() {
    39  		if recover() == nil {
    40  			panic("function should panic")
    41  		}
    42  	}()
    43  	f()
    44  }