Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "go/ast"
- "golang.org/x/tools/go/packages"
- "reflect"
- "time"
- "unicode"
- )
- func printTypes(){
- config := &packages.Config{
- Mode: packages.NeedSyntax,
- }
- // you can use any package, for local package use name defined in go.mod
- pkgs, _ := packages.Load(config, "time")
- pkg := pkgs[0]
- for _, s := range pkg.Syntax {
- for n, o := range s.Scope.Objects {
- if o.Kind == ast.Typ {
- // omit that `if` if you check local types
- if unicode.IsUpper([]rune(n)[0]) {
- // note that reflect.ValueOf(*new(%s)) won't work with interfaces
- fmt.Printf(" new(time.%s),\n", n)
- }
- }
- }
- }
- }
- func printTypesInfo() {
- for _, p := range []interface{}{
- // this list was generated by printTypes
- new(time.ParseError),
- new(time.Timer),
- new(time.Ticker),
- new(time.Time),
- new(time.Duration),
- new(time.Month),
- new(time.Weekday),
- new(time.Location),
- } {
- // dump info about each type(that's why I googled "get all types in package")
- t := reflect.TypeOf(p).Elem()
- fmt.Println(t.Kind(), t.Name(), t.Size(), t.Align())
- if t.Kind() == reflect.Struct {
- for i :=0;i<t.NumField();i++{
- f := t.Field(i)
- fmt.Println(" ", f.Index, f.Name, f.Type, f.Offset, f.Type.Size())
- }
- }
- }
- }
- func main() {
- printTypes()
- printTypesInfo()
- }
- /* output for package time
- struct ParseError 80 8
- [0] Layout string 0 16
- [1] Value string 16 16
- [2] LayoutElem string 32 16
- [3] ValueElem string 48 16
- [4] Message string 64 16
- struct Timer 80 8
- [0] C <-chan time.Time 0 8
- [1] r time.runtimeTimer 8 72
- struct Ticker 80 8
- [0] C <-chan time.Time 0 8
- [1] r time.runtimeTimer 8 72
- struct Time 24 8
- [0] wall uint64 0 8
- [1] ext int64 8 8
- [2] loc *time.Location 16 8
- int64 Duration 8 8
- int Month 8 8
- int Weekday 8 8
- struct Location 104 8
- [0] name string 0 16
- [1] zone []time.zone 16 24
- [2] tx []time.zoneTrans 40 24
- [3] extend string 64 16
- [4] cacheStart int64 80 8
- [5] cacheEnd int64 88 8
- [6] cacheZone *time.zone 96 8
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement