github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/test/interface/fail.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  // Test that interface conversion fails when method is missing.
     8  
     9  package main
    10  
    11  type I interface {
    12  	Foo()
    13  }
    14  
    15  func main() {
    16  	shouldPanic(p1)
    17  	shouldPanic(p2)
    18  }
    19  
    20  func p1() {
    21  	var s *S
    22  	var i I
    23  	var e interface{}
    24  	e = s
    25  	i = e.(I)
    26  	_ = i
    27  }
    28  
    29  type S struct{}
    30  
    31  func (s *S) _() {}
    32  
    33  type B interface {
    34  	_()
    35  }
    36  
    37  func p2() {
    38  	var s *S
    39  	var b B
    40  	var e interface{}
    41  	e = s
    42  	b = e.(B)
    43  	_ = b
    44  }
    45  
    46  func shouldPanic(f func()) {
    47  	defer func() {
    48  		if recover() == nil {
    49  			panic("function should panic")
    50  		}
    51  	}()
    52  	f()
    53  }