Load loads the given package with all dependencies (including the runtime package). Call .Parse() afterwards to parse all Go files (including CGo processing, if necessary).
(config *compileopts.Config, inputPkg string, typeChecker types.Config)
| 105 | // package). Call .Parse() afterwards to parse all Go files (including CGo |
| 106 | // processing, if necessary). |
| 107 | func Load(config *compileopts.Config, inputPkg string, typeChecker types.Config) (*Program, error) { |
| 108 | goroot, err := GetCachedGoroot(config) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | var wd string |
| 113 | if config.Options.Directory != "" { |
| 114 | wd = config.Options.Directory |
| 115 | } else { |
| 116 | wd, err = os.Getwd() |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | } |
| 121 | p := &Program{ |
| 122 | config: config, |
| 123 | typeChecker: typeChecker, |
| 124 | goroot: goroot, |
| 125 | workingDir: wd, |
| 126 | Packages: make(map[string]*Package), |
| 127 | fset: token.NewFileSet(), |
| 128 | } |
| 129 | |
| 130 | // List the dependencies of this package, in raw JSON format. |
| 131 | extraArgs := []string{"-json", "-deps", "-e"} |
| 132 | if config.TestConfig.CompileTestBinary { |
| 133 | extraArgs = append(extraArgs, "-test") |
| 134 | } |
| 135 | cmd, err := List(config, extraArgs, []string{inputPkg}) |
| 136 | if err != nil { |
| 137 | return nil, err |
| 138 | } |
| 139 | buf := &bytes.Buffer{} |
| 140 | cmd.Stdout = buf |
| 141 | cmd.Stderr = os.Stderr |
| 142 | err = cmd.Run() |
| 143 | if err != nil { |
| 144 | if exitErr, ok := err.(*exec.ExitError); ok { |
| 145 | os.Exit(exitErr.ExitCode()) |
| 146 | } |
| 147 | return nil, fmt.Errorf("failed to run `go list`: %s", err) |
| 148 | } |
| 149 | |
| 150 | // Parse the returned json from `go list`. |
| 151 | decoder := json.NewDecoder(buf) |
| 152 | var pkgErrors []error |
| 153 | for { |
| 154 | pkg := &Package{ |
| 155 | program: p, |
| 156 | FileHashes: make(map[string][]byte), |
| 157 | EmbedGlobals: make(map[string][]*EmbedFile), |
| 158 | info: types.Info{ |
| 159 | Types: make(map[ast.Expr]types.TypeAndValue), |
| 160 | Instances: make(map[*ast.Ident]types.Instance), |
| 161 | Defs: make(map[*ast.Ident]types.Object), |
| 162 | Uses: make(map[*ast.Ident]types.Object), |
| 163 | Implicits: make(map[ast.Node]types.Object), |
| 164 | Scopes: make(map[ast.Node]*types.Scope), |