github.com/dearplain/goloader@v0.0.0-20190107071432-2b1e47d74273/examples/base/base.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "runtime" 6 ) 7 8 type Vertex struct { 9 X, Y int 10 } 11 12 func (v *Vertex) Print() { 13 fmt.Println("print", v) 14 } 15 16 type PrintInf interface { 17 Print() 18 } 19 20 func main() { 21 22 var ( 23 v1 = Vertex{1, 2} 24 v2 = Vertex{X: 1} 25 v3 = Vertex{} 26 p = &Vertex{1, 2} 27 ) 28 29 fmt.Println(Vertex{1, 2}) 30 fmt.Println(v1, p, v2, v3) 31 fmt.Printf("%#v %#v %#v %#v\n", v1, p, v2, v3) 32 33 var inf PrintInf = p 34 inf.Print() 35 36 { 37 names := [4]string{ 38 "John", 39 "Paul", 40 "George", 41 "Ringo", 42 } 43 fmt.Println(names) 44 } 45 46 { 47 s := []struct { 48 i int 49 b bool 50 }{ 51 {2, true}, 52 {3, false}, 53 } 54 fmt.Println(s) 55 } 56 57 { 58 var m = map[string]Vertex{ 59 "Bell Labs": Vertex{ 60 40, -74, 61 }, 62 "Google": Vertex{ 63 37, -122, 64 }, 65 } 66 fmt.Println(m) 67 } 68 69 { 70 whatAmI := func(i interface{}) { 71 switch t := i.(type) { 72 case bool: 73 fmt.Println("I'm a bool") 74 case int: 75 fmt.Println("I'm an int") 76 default: 77 fmt.Printf("Don't know type %T\n", t) 78 } 79 } 80 go whatAmI(true) 81 go whatAmI(1) 82 whatAmI("hey") 83 } 84 85 recoverTest() 86 87 { 88 pos, neg := adder(), adder() 89 for i := 0; i < 10; i++ { 90 fmt.Println( 91 pos(i), 92 neg(-2*i), 93 ) 94 } 95 } 96 97 } 98 99 func adder() func(int) int { 100 sum := 0 101 return func(x int) int { 102 sum += x 103 return sum 104 } 105 } 106 107 func recoverTest() { 108 defer logPanic() 109 panic("this is a panic test") 110 } 111 112 func logPanic() { 113 if r := recover(); r != nil { 114 trace := make([]byte, 1024) 115 count := runtime.Stack(trace, false) 116 fmt.Printf("panic: %s\nStack of %d bytes:\n%s\n", r, count, trace) 117 } 118 }