Git Product home page Git Product logo

importer's Introduction

importer

importer for go/types package

It makes go/types.Config.Check usable with imported packages that have not been installed yet (fixes golang/go#14496)

Documentation

see http://godoc.org/github.com/metakeule/importer

Example

package main

import (
    "fmt"
    "github.com/metakeule/importer"
    "go/ast"
    "go/build"
    "go/token"
    "go/types"
    "os"
    "path/filepath"
)

func mkInfo() types.Info {
    return types.Info{
        Types: make(map[ast.Expr]types.TypeAndValue),
        Defs:  make(map[*ast.Ident]types.Object),
        Uses:  make(map[*ast.Ident]types.Object),
    }
}

func mkConfig() types.Config {
    return types.Config{
        IgnoreFuncBodies: true,
    }
}

func panicOnErr(err error) {
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    var (
        pkgpath = "github.com/metakeule/importer"

        // assumes a single GOPATH directory
        srcDir = filepath.Join(os.Getenv("GOPATH"), "src", pkgpath)

        fset     = token.NewFileSet()
        astFiles []*ast.File
    )

    buildPkg, err := build.Import(pkgpath, srcDir, 0)
    panicOnErr(err)

    astFiles, err = importer.ParseAstFiles(fset, buildPkg.Dir, append(buildPkg.GoFiles, buildPkg.CgoFiles...))

    panicOnErr(err)

    info, config := mkInfo(), mkConfig()
    // here the importer is used with the same config and info settings
    config.Importer = importer.CheckImporter(mkInfo, mkConfig)
    _, err = (&config).Check(buildPkg.ImportPath, fset, astFiles, &info)

    panicOnErr(err)

    for ident, obj := range info.Defs {
        if ident.IsExported() {
            fmt.Printf("%s:\n    %s\n", ident.Name, obj.Type())
        }
    }
}

importer's People

Contributors

metakeule avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.