Index: 2014/static-analysis/demo.go |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/2014/static-analysis/demo.go |
@@ -0,0 +1,41 @@ |
+package main |
+ |
+import "fmt" |
+ |
+type Leaf int |
+ |
+func (l Leaf) Sum() int { return int(l) } |
+func (l Leaf) String() string { return fmt.Sprintf("%d", l) } |
+ |
+type Branch struct{ left, rhs Tree } |
+ |
+func (b *Branch) Sum() int { return b.left.Sum() + b.rhs.Sum() } |
+func (b *Branch) String() string { return fmt.Sprintf("(%s, %s)", b.left, b.rhs) } |
+ |
+type Tree interface { |
+ Sum() int |
+} |
+ |
+func main() { |
+ var tree Tree = Leaf(42) |
+ fmt.Println(tree.Sum()) |
+ |
+ if unknown { |
+ tree = &Branch{tree, Leaf(123)} |
+ } |
+ fmt.Println(tree.Sum()) |
+ fmt.Println(tree) |
+} |
+ |
+var unknown bool |
+ |
+// |
+ |
+func _() { |
+ type Answer struct{ right bool } |
+ var x struct { |
+ Answer |
+ Branch |
+ } |
+ fmt.Println(x.right) |
+} |