Skip to content

go/types: add Info.PkgName method #62037

Closed
@adonovan

Description

@adonovan

Any time a client of go/types needs to find the types.PkgName (or just the name) defined by an import, they must write logic equivalent to this:

// PkgName returns the local package name defined by the specified import.
func (info *types.Info) PkgName(imp *ast.ImportSpec) (*types.PkgName, bool) {
	var obj types.Object
	if imp.Name != nil {
		obj = info.Defs[imp.Name]
	} else {
		obj = info.Implicits[imp]
	}
	pkgname, ok := obj.(*types.PkgName)
	return pkgname, ok
}

I propose that we add this method to types.Info.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions