Advertisement
Guest User

Go print info about all package types

a guest
Sep 1st, 2021
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.06 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "go/ast"
  6.     "golang.org/x/tools/go/packages"
  7.     "reflect"
  8.     "time"
  9.     "unicode"
  10. )
  11.  
  12. func printTypes(){
  13.     config := &packages.Config{
  14.         Mode:  packages.NeedSyntax,
  15.     }
  16.     // you can use any package, for local package use name defined in go.mod
  17.     pkgs, _ := packages.Load(config, "time")
  18.     pkg := pkgs[0]
  19.  
  20.     for _, s := range pkg.Syntax {
  21.         for n, o := range s.Scope.Objects {
  22.             if o.Kind == ast.Typ {
  23.                 // omit that `if` if you check local types
  24.                 if unicode.IsUpper([]rune(n)[0]) {
  25.                     // note that reflect.ValueOf(*new(%s)) won't work with interfaces
  26.                     fmt.Printf("    new(time.%s),\n", n)
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. func printTypesInfo()  {
  34.     for _, p := range []interface{}{
  35.         // this list was generated by printTypes
  36.         new(time.ParseError),
  37.         new(time.Timer),
  38.         new(time.Ticker),
  39.         new(time.Time),
  40.         new(time.Duration),
  41.         new(time.Month),
  42.         new(time.Weekday),
  43.         new(time.Location),
  44.     } {
  45.         // dump info about each type(that's why I googled "get all types in package")
  46.         t := reflect.TypeOf(p).Elem()
  47.         fmt.Println(t.Kind(), t.Name(), t.Size(), t.Align())
  48.         if t.Kind() == reflect.Struct {
  49.             for i :=0;i<t.NumField();i++{
  50.                 f := t.Field(i)
  51.                 fmt.Println("  ", f.Index, f.Name, f.Type, f.Offset, f.Type.Size())
  52.             }
  53.         }
  54.     }
  55.  
  56. }
  57.  
  58. func main() {
  59.     printTypes()
  60.     printTypesInfo()
  61. }
  62. /* output for package time
  63. struct ParseError 80 8
  64.    [0] Layout string 0 16
  65.    [1] Value string 16 16
  66.    [2] LayoutElem string 32 16
  67.    [3] ValueElem string 48 16
  68.    [4] Message string 64 16
  69. struct Timer 80 8
  70.    [0] C <-chan time.Time 0 8
  71.    [1] r time.runtimeTimer 8 72
  72. struct Ticker 80 8
  73.    [0] C <-chan time.Time 0 8
  74.    [1] r time.runtimeTimer 8 72
  75. struct Time 24 8
  76.    [0] wall uint64 0 8
  77.    [1] ext int64 8 8
  78.    [2] loc *time.Location 16 8
  79. int64 Duration 8 8
  80. int Month 8 8
  81. int Weekday 8 8
  82. struct Location 104 8
  83.    [0] name string 0 16
  84.    [1] zone []time.zone 16 24
  85.    [2] tx []time.zoneTrans 40 24
  86.    [3] extend string 64 16
  87.    [4] cacheStart int64 80 8
  88.    [5] cacheEnd int64 88 8
  89.    [6] cacheZone *time.zone 96 8
  90. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement