本文整理汇总了Golang中go/types.Package类的典型用法代码示例。如果您正苦于以下问题:Golang Package类的具体用法?Golang Package怎么用?Golang Package使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Package类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: namePrefixOf
func (g *objcGen) namePrefixOf(pkg *types.Package) string {
p := g.prefix
if p == "" {
p = "Go"
}
return p + strings.Title(pkg.Name())
}
示例2: defaultFileName
func defaultFileName(lang string, pkg *types.Package) string {
switch lang {
case "java":
if pkg == nil {
return "Universe.java"
}
firstRune, size := utf8.DecodeRuneInString(pkg.Name())
className := string(unicode.ToUpper(firstRune)) + pkg.Name()[size:]
return className + ".java"
case "go":
if pkg == nil {
return "go_universe.go"
}
return "go_" + pkg.Name() + ".go"
case "objc":
if pkg == nil {
return "GoUniverse.m"
}
firstRune, size := utf8.DecodeRuneInString(pkg.Name())
className := string(unicode.ToUpper(firstRune)) + pkg.Name()[size:]
return "Go" + className + ".m"
}
errorf("unknown target language: %q", lang)
os.Exit(exitStatus)
return ""
}
示例3: pkg
func (p *exporter) pkg(pkg *types.Package, emptypath bool) {
if pkg == nil {
log.Fatalf("gcimporter: unexpected nil pkg")
}
// if we saw the package before, write its index (>= 0)
if i, ok := p.pkgIndex[pkg]; ok {
p.index('P', i)
return
}
// otherwise, remember the package, write the package tag (< 0) and package data
if trace {
p.tracef("P%d = { ", len(p.pkgIndex))
defer p.tracef("} ")
}
p.pkgIndex[pkg] = len(p.pkgIndex)
p.tag(packageTag)
p.string(pkg.Name())
if emptypath {
p.string("")
} else {
p.string(pkg.Path())
}
}
示例4: Qualifier
func (p *Parser) Qualifier(pkg *types.Package) string {
if pkg.Name() == p.Package {
return ""
}
return pkg.Name()
}
示例5: FindQueryMethods
// FindQueryMethods locates all methods in the given package (assumed to be
// package database/sql) with a string parameter named "query".
func FindQueryMethods(sql *types.Package, ssa *ssa.Program) []*QueryMethod {
methods := make([]*QueryMethod, 0)
scope := sql.Scope()
for _, name := range scope.Names() {
o := scope.Lookup(name)
if !o.Exported() {
continue
}
if _, ok := o.(*types.TypeName); !ok {
continue
}
n := o.Type().(*types.Named)
for i := 0; i < n.NumMethods(); i++ {
m := n.Method(i)
if !m.Exported() {
continue
}
s := m.Type().(*types.Signature)
if num, ok := FuncHasQuery(s); ok {
methods = append(methods, &QueryMethod{
Func: m,
SSA: ssa.FuncValue(m),
ArgCount: s.Params().Len(),
Param: num,
})
}
}
}
return methods
}
示例6: GenGo
func (b *binder) GenGo(pkg *types.Package, allPkg []*types.Package, outdir string) error {
pkgName := "go_"
pkgPath := ""
if pkg != nil {
pkgName += pkg.Name()
pkgPath = pkg.Path()
}
goFile := filepath.Join(outdir, pkgName+"main.go")
generate := func(w io.Writer) error {
if buildX {
printcmd("gobind -lang=go -outdir=%s %s", outdir, pkgPath)
}
if buildN {
return nil
}
conf := &bind.GeneratorConfig{
Writer: w,
Fset: b.fset,
Pkg: pkg,
AllPkg: allPkg,
}
return bind.GenGo(conf)
}
if err := writeFile(goFile, generate); err != nil {
return err
}
return nil
}
示例7: pkgPrefix
// pkgPrefix returns a prefix that disambiguates symbol names for binding
// multiple packages.
//
// TODO(elias.naur): Avoid (and test) name clashes from multiple packages
// with the same name. Perhaps use the index from the order the package is
// generated.
func pkgPrefix(pkg *types.Package) string {
// The error type has no package
if pkg == nil {
return ""
}
return pkg.Name()
}
示例8: find
func find(pkg *types.Package, iface string) (*types.Interface, error) {
scope := pkg.Scope()
names := scope.Names()
for _, n := range names {
obj := scope.Lookup(n)
tn, ok := obj.(*types.TypeName)
if !ok {
continue
}
if tn.Name() != iface {
continue
}
if !obj.Exported() {
return nil, fmt.Errorf("%s should exported", iface)
}
t := tn.Type().Underlying()
i, ok := t.(*types.Interface)
if !ok {
return nil, fmt.Errorf("exptected interface, got %s for %s", t, iface)
}
return i, nil
}
return nil, fmt.Errorf("%s not found in %s", iface, pkg.Name())
}
示例9: Collect
//Collect going through package and collect info
//using conf.Check method. It's using this implementation
//of importer for check all inner packages and go/types/importer.Default()
//to check all built in packages (without sources)
func (_importer *CollectInfoImporter) Collect() (*types.Package, *token.FileSet, error) {
var conf types.Config
conf.Importer = _importer
conf.Error = _importer.errorHandler
if _importer.packages == nil {
_importer.packages = make(map[string]*types.Package)
}
var pkg *types.Package
var err error
var files []string
if files, err = fs.SourceFiles(_importer.Pkg, false); err != nil {
return nil, nil, err
}
if _importer.fset, _importer.astFiles, err = doParseFiles(files, _importer.fset); err != nil {
return nil, nil, err
}
//XXX: return positive result if check() returns error.
pkg, _ = conf.Check(_importer.Pkg, _importer.fset, _importer.astFiles, _importer.Info)
// if pkg, err = conf.Check(_importer.Pkg, _importer.fset, _importer.astFiles, _importer.Info); err != nil {
// return pkg, _importer.fset, err
// }
_importer.packages[_importer.Pkg] = pkg
util.Debug("package [%s] successfully parsed\n", pkg.Name())
return pkg, _importer.fset, nil
}
示例10: pkgName
// pkgName retuns the package name and adds the package to the list of
// imports.
func (g *goGen) pkgName(pkg *types.Package) string {
// The error type has no package
if pkg == nil {
return ""
}
g.imports[pkg.Path()] = struct{}{}
return pkg.Name() + "."
}
示例11: getMethods
func getMethods(pkg *types.Package, typename string) map[string]*types.Func {
r := make(map[string]*types.Func)
mset := types.NewMethodSet(types.NewPointer(pkg.Scope().Lookup(typename).Type()))
for i := 0; i < mset.Len(); i++ {
fn := mset.At(i).Obj().(*types.Func)
r[fn.Name()] = fn
}
return r
}
示例12: newBinder
func newBinder(p *types.Package) (*binder, error) {
if p.Name() == "main" {
return nil, fmt.Errorf("package %q: can only bind a library package", p.Name())
}
b := &binder{
fset: token.NewFileSet(),
pkg: p,
}
return b, nil
}
示例13: namePrefixOf
func (g *ObjcGen) namePrefixOf(pkg *types.Package) string {
if pkg == nil {
return "GoUniverse"
}
p := g.Prefix
if p == "" {
p = "Go"
}
return p + strings.Title(pkg.Name())
}
示例14: joinQuery
func joinQuery(pkg *types.Package, parent types.Object, obj types.Object, suffix string) string {
var args []string
args = append(args, pkg.Name())
if parent != nil {
args = append(args, parent.Name())
}
args = append(args, nameExported(obj.Name()))
return strings.Join(args, ".") + suffix
}
示例15: pkgFirstElem
func pkgFirstElem(p *types.Package) string {
if p == nil {
return ""
}
path := p.Path()
idx := strings.Index(path, "/")
if idx == -1 {
return ""
}
return path[:idx]
}