github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/go/importer/importer.go (about)

     1  // Copyright 2015 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 importer provides access to export data importers.
     6  package importer
     7  
     8  import (
     9  	"go/internal/gcimporter"
    10  	"go/types"
    11  	"io"
    12  	"runtime"
    13  )
    14  
    15  // A Lookup function returns a reader to access package data for
    16  // a given import path, or an error if no matching package is found.
    17  type Lookup func(path string) (io.ReadCloser, error)
    18  
    19  // For returns an Importer for the given compiler and lookup interface,
    20  // or nil. Supported compilers are "gc", and "gccgo". If lookup is nil,
    21  // the default package lookup mechanism for the given compiler is used.
    22  func For(compiler string, lookup Lookup) types.Importer {
    23  	switch compiler {
    24  	case "gc":
    25  		if lookup == nil {
    26  			return make(gcimports)
    27  		}
    28  		panic("gc importer for custom import path lookup not yet implemented")
    29  	case "gccgo":
    30  		// TODO(gri) We have the code. Plug it in.
    31  		panic("gccgo importer unimplemented")
    32  	}
    33  	// compiler not supported
    34  	return nil
    35  }
    36  
    37  // Default returns an Importer for the compiler that built the running binary.
    38  func Default() types.Importer {
    39  	return For(runtime.Compiler, nil)
    40  }
    41  
    42  type gcimports map[string]*types.Package
    43  
    44  func (m gcimports) Import(path string) (*types.Package, error) {
    45  	return gcimporter.Import(m, path)
    46  }