github.com/podhmo/reflect-shape@v0.4.3/_examples/motivation/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "reflect" 7 8 reflectshape "github.com/podhmo/reflect-shape" 9 ) 10 11 func main() { 12 motivation1() 13 fmt.Println("\n========================================\n") 14 motivation2() 15 fmt.Println("\n========================================\n") 16 motivation3() 17 } 18 19 func motivation1() { 20 // with reflect 21 { 22 fmt.Println("reflect.TypeOf() pkgpath:", reflect.TypeOf(http.ListenAndServe).PkgPath()) 23 // Output: reflect.TypeOf() pkgpath: 24 25 fmt.Println("reflect.TypeOf() pkgpath:", reflect.TypeOf(http.Client{}).PkgPath()) 26 // Output: reflect.TypeOf() pkgpath: net/http 27 fmt.Println("reflect.TypeOf() pkgpath:", reflect.TypeOf(&http.Client{}).PkgPath()) 28 // Output: reflect.TypeOf() pkgpath: 29 } 30 fmt.Println("----------------------------------------") 31 32 // with reflect-shape 33 { 34 fmt.Println("reflect-shape pkgpath:", cfg.Extract(http.ListenAndServe).Package.Path) 35 // Output: reflect-shape pkgpath: net/http 36 37 fmt.Println("reflect-shape pkgpath:", cfg.Extract(&http.Client{}).Package.Path) 38 // Output: reflect-shape pkgpath: net/http 39 } 40 } 41 42 func motivation2() { 43 foo := func(x, y int) {} 44 bar := func(x, y int) {} 45 46 // with reflect 47 { 48 fmt.Println("reflect.TypeOf() id: foo == foo?", reflect.TypeOf(foo) == reflect.TypeOf(foo)) 49 // Output: reflect.TypeOf() id: foo == foo? true 50 fmt.Println("reflect.TypeOf() id: foo == bar?", reflect.TypeOf(foo) == reflect.TypeOf(bar)) 51 // Output: reflect.TypeOf() id: foo == bar? true 52 } 53 fmt.Println("----------------------------------------") 54 55 // with reflect-shape 56 { 57 fmt.Println("reflect-shape id: foo == foo?", cfg.Extract(foo).ID == cfg.Extract(foo).ID) 58 // Output: reflect-shape id: foo == foo? true 59 fmt.Println("reflect-shape id: foo == bar?", cfg.Extract(foo).ID == cfg.Extract(bar).ID) 60 // Output: reflect-shape id: foo == bar? false 61 62 // or cfg.Extract(foo).Equal(cfg.Extract(bar)) 63 } 64 } 65 66 // This is Hello 67 func Hello( 68 name string, // name of target 69 ) { 70 fmt.Println("hello", name) 71 } 72 73 // This is Bar 74 type Bar struct { 75 Name string // name of Bar 76 } 77 78 func motivation3() { 79 { 80 shape := cfg.Extract(Hello) 81 fmt.Println("Name", shape.Name, "kind", shape.Kind, "Doc", shape.Func().Doc()) 82 for _, a := range shape.Func().Args() { 83 fmt.Println("--", "Arg", a.Name, "kind", a.Shape.Kind, "Doc", a.Doc) 84 } 85 // Output: Name Hello kind func Doc This is Hello 86 // -- Arg name kind string Doc name of target 87 } 88 { 89 shape := cfg.Extract(&Bar{}) 90 fmt.Println("Name", shape.Name, "kind", shape.Kind, "Doc", shape.Struct().Doc()) 91 for _, f := range shape.Struct().Fields() { 92 fmt.Println("--", "Field", f.Name, "kind", f.Shape.Kind, "Doc", f.Doc) 93 } 94 // Output: Name Bar kind struct Doc This is Bar 95 // -- Field Name kind string Doc name of Bar 96 } 97 } 98 99 var cfg = &reflectshape.Config{}