Standard library / go/types
go/types.ImporterFrom
Extends Importer with source-directory and reserved mode information. Repeated requests for a path and directory must return the same package. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
type ImporterFrom interface { Importer
ImportFrom(path, dir string, mode ImportMode) (*Package, error)}Working Example
Example Snippetgo
package main
import ( "fmt" "go/types")
func check(e error) { if e != nil { panic(e) }}
type localImporter struct{ p *types.Package }
func (i localImporter) Import(path string) (*types.Package, error) { return i.ImportFrom(path, "", 0) }func (i localImporter) ImportFrom(path, dir string, mode types.ImportMode) (*types.Package, error) { if mode != 0 || path != i.p.Path() { return nil, fmt.Errorf("unsupported import") } return i.p, nil}
func main() { p := types.NewPackage("dep", "dep") p.MarkComplete() var i types.ImporterFrom = localImporter{p} a, e := i.ImportFrom("dep", ".", 0) check(e) b, e := i.ImportFrom("dep", ".", 0) check(e) fmt.Println(a == b)}