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

     1  // run
     2  
     3  // Copyright 2011 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 run-time error detection for interface values containing types
     8  // that cannot be compared for equality.
     9  
    10  package main
    11  
    12  func main() {
    13  	cmp(1)
    14  
    15  	var (
    16  		m map[int]int
    17  		s struct{ x []int }
    18  		f func()
    19  	)
    20  	noCmp(m)
    21  	noCmp(s)
    22  	noCmp(f)
    23  }
    24  
    25  func cmp(x interface{}) bool {
    26  	return x == x
    27  }
    28  
    29  func noCmp(x interface{}) {
    30  	shouldPanic(func() { cmp(x) })
    31  }
    32  
    33  func shouldPanic(f func()) {
    34  	defer func() {
    35  		if recover() == nil {
    36  			panic("function should panic")
    37  		}
    38  	}()
    39  	f()
    40  }