github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/go/internal/srcimporter/srcimporter.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package srcimporter implements importing directly
     6  // from source files rather than installed packages.
     7  package srcimporter
     8  
     9  import (
    10  	"github.com/shogo82148/std/go/build"
    11  	"github.com/shogo82148/std/go/token"
    12  	"github.com/shogo82148/std/go/types"
    13  )
    14  
    15  // An Importer provides the context for importing packages from source code.
    16  type Importer struct {
    17  	ctxt     *build.Context
    18  	fset     *token.FileSet
    19  	sizes    types.Sizes
    20  	packages map[string]*types.Package
    21  }
    22  
    23  // New returns a new Importer for the given context, file set, and map
    24  // of packages. The context is used to resolve import paths to package paths,
    25  // and identifying the files belonging to the package. If the context provides
    26  // non-nil file system functions, they are used instead of the regular package
    27  // os functions. The file set is used to track position information of package
    28  // files; and imported packages are added to the packages map.
    29  func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Package) *Importer
    30  
    31  // Import(path) is a shortcut for ImportFrom(path, ".", 0).
    32  func (p *Importer) Import(path string) (*types.Package, error)
    33  
    34  // ImportFrom imports the package with the given import path resolved from the given srcDir,
    35  // adds the new package to the set of packages maintained by the importer, and returns the
    36  // package. Package path resolution and file system operations are controlled by the context
    37  // maintained with the importer. The import mode must be zero but is otherwise ignored.
    38  // Packages that are not comprised entirely of pure Go files may fail to import because the
    39  // type checker may not be able to determine all exported entities (e.g. due to cgo dependencies).
    40  func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error)