You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// An ImporterFunc is an implementation of the single-method
// types.Importer interface based on a function value.
type ImporterFunc func(path string) (*types.Package, error)
func (f ImporterFunc) Import(path string) (*types.Package, error) { return f(path) }
since we seem to keep typing them out everywhere else:
Note that it works with the ImportFrom method, and since ImporterFrom is documented as The types package does not call Import if an ImporterFrom is present, I make Import simply panic as I expect it to never be called.
Shouldn't that be the added API, e.g. as ImportFromFunc? Perhaps some of your use cases don't care about vendored packages, but in general I assume that the Import method should be treated as obsolete in favor of ImportFrom. For example:
// An ImporterFromFunc is an implementation of the single-method
// types.ImporterFrom interface based on a function value.
type ImporterFromFunc func(path, dir string, mode types.ImportMode) (*types.Package, error)
func (f ImporterFromFunc) Import(path string) (*types.Package, error) {
panic("Import should never be called when ImportFrom is present")
// or alternatively...
// return f(path, "", 0)
}
func (f ImporterFromFunc) ImportFrom(path, dir string, mode types.ImportMode) (*types.Package, error) {
return f(path, dir, mode)
}
@mvdan I think the in the common case we already know the mapping of package path -> package, because we've loaded it from go/packages and/or are type checking a single package. Therefore I expect the API @adonovan proposes is more likely to be useful, as suggested by the cited reproductions.
I propose to add these two lines to
go/types
:since we seem to keep typing them out everywhere else:
@gri @findleyr
The text was updated successfully, but these errors were encountered: