github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/fixedbugs/bug099.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  // Interface
    10  type I interface { F() int }
    11  
    12  // Implements interface
    13  type S struct { }
    14  func (s *S) F() int { return 1 }
    15  
    16  // Allocates S but returns I
    17  // Arg is unused but important:
    18  // if you take it out (and the 0s below)
    19  // then the bug goes away.
    20  func NewI(i int) I {
    21  	return new(S)
    22  }
    23  
    24  // Uses interface method.
    25  func Use(x I) {
    26  	x.F()
    27  }
    28  
    29  func main() {
    30  	i := NewI(0);
    31  	Use(i);
    32  
    33  	// Again, without temporary
    34  	// Crashes because x.F is 0.
    35  	Use(NewI(0));
    36  }
    37