本文整理汇总了Golang中golang.org/x/tools/go/types.PkgName类的典型用法代码示例。如果您正苦于以下问题:Golang PkgName类的具体用法?Golang PkgName怎么用?Golang PkgName使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PkgName类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: describePackage
func describePackage(o *Oracle, qpos *QueryPos, path []ast.Node) (*describePackageResult, error) {
var description string
var pkg *types.Package
switch n := path[0].(type) {
case *ast.ImportSpec:
var pkgname *types.PkgName
if n.Name != nil {
pkgname = qpos.info.Defs[n.Name].(*types.PkgName)
} else if p := qpos.info.Implicits[n]; p != nil {
pkgname = p.(*types.PkgName)
}
pkg = pkgname.Imported()
description = fmt.Sprintf("import of package %q", pkg.Path())
case *ast.Ident:
if _, isDef := path[1].(*ast.File); isDef {
// e.g. package id
pkg = qpos.info.Pkg
description = fmt.Sprintf("definition of package %q", pkg.Path())
} else {
// e.g. import id "..."
// or id.F()
pkg = qpos.info.ObjectOf(n).(*types.PkgName).Imported()
description = fmt.Sprintf("reference to package %q", pkg.Path())
}
default:
// Unreachable?
return nil, fmt.Errorf("unexpected AST for package: %T", n)
}
var members []*describeMember
// NB: "unsafe" has no types.Package
if pkg != nil {
// Enumerate the accessible package members
// in lexicographic order.
for _, name := range pkg.Scope().Names() {
if pkg == qpos.info.Pkg || ast.IsExported(name) {
mem := pkg.Scope().Lookup(name)
var methods []*types.Selection
if mem, ok := mem.(*types.TypeName); ok {
methods = accessibleMethods(mem.Type(), qpos.info.Pkg)
}
members = append(members, &describeMember{
mem,
methods,
})
}
}
}
return &describePackageResult{o.fset, path[0], description, pkg, members}, nil
}
开发者ID:Lane391,项目名称:golangdemo,代码行数:54,代码来源:describe.go
示例2: checkInFileBlock
// checkInFileBlock performs safety checks for renames of objects in the file block,
// i.e. imported package names.
func (r *Unexporter) checkInFileBlock(objsToUpdate map[types.Object]string, from *types.PkgName, to string) {
// Check import name is not "init".
if to == "init" {
r.errorf(from.Pos(), "%q is not a valid imported package name", to)
}
// Check for conflicts between file and package block.
if prev := from.Pkg().Scope().Lookup(to); prev != nil {
r.warn(from,
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict",
objectKind(from), from.Name(), to),
r.errorf(prev.Pos(), "\twith this package member %s",
objectKind(prev)))
return // since checkInPackageBlock would report redundant errors
}
// Check for conflicts in lexical scope.
r.checkInLexicalScope(objsToUpdate, from, to, r.packages[from.Pkg()])
// Finally, modify ImportSpec syntax to add or remove the Name as needed.
info, path, _ := r.iprog.PathEnclosingInterval(from.Pos(), from.Pos())
if from.Imported().Name() == to {
// ImportSpec.Name not needed
path[1].(*ast.ImportSpec).Name = nil
} else {
// ImportSpec.Name needed
if spec := path[1].(*ast.ImportSpec); spec.Name == nil {
spec.Name = &ast.Ident{NamePos: spec.Path.Pos(), Name: to}
info.Defs[spec.Name] = from
}
}
}
开发者ID:rakyll,项目名称:GCSolutions,代码行数:34,代码来源:check.go
注:本文中的golang.org/x/tools/go/types.PkgName类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论