github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/static-analysis/demo.go (about)

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  type Leaf int
     8  
     9  func (l Leaf) Sum() int       { return int(l) }
    10  func (l Leaf) String() string { return fmt.Sprintf("%d", l) }
    11  
    12  type Branch struct{ left, rhs Tree }
    13  
    14  func (b *Branch) Sum() int       { return b.left.Sum() + b.rhs.Sum() }
    15  func (b *Branch) String() string { return fmt.Sprintf("(%s, %s)", b.left, b.rhs) }
    16  
    17  type Tree interface {
    18  	Sum() int
    19  }
    20  
    21  func main() {
    22  	var tree Tree = Leaf(42)
    23  	fmt.Println(tree.Sum())
    24  
    25  	if unknown {
    26  		tree = &Branch{tree, Leaf(123)}
    27  	}
    28  	fmt.Println(tree.Sum())
    29  	fmt.Println(tree)
    30  }
    31  
    32  var unknown bool
    33  
    34  //
    35  
    36  func _() {
    37  	type Answer struct{ right bool }
    38  	var x struct {
    39  		Answer
    40  		Branch
    41  	}
    42  	fmt.Println(x.right)
    43  }