Load loads and returns the Go packages named by the given patterns. The cfg parameter specifies loading options; nil behaves the same as an empty [Config]. The [Config.Mode] field is a set of bits that determine what kinds of information should be computed and returned. Modes that require more inf
(cfg *Config, patterns ...string)
| 259 | // proceeding with further analysis. The [PrintErrors] function is |
| 260 | // provided for convenient display of all errors. |
| 261 | func Load(cfg *Config, patterns ...string) ([]*Package, error) { |
| 262 | ld := newLoader(cfg) |
| 263 | response, external, err := defaultDriver(&ld.Config, patterns...) |
| 264 | if err != nil { |
| 265 | return nil, err |
| 266 | } |
| 267 | |
| 268 | ld.sizes = types.SizesFor(response.Compiler, response.Arch) |
| 269 | if ld.sizes == nil && ld.Config.Mode&(NeedTypes|NeedTypesSizes|NeedTypesInfo) != 0 { |
| 270 | // Type size information is needed but unavailable. |
| 271 | if external { |
| 272 | // An external driver may fail to populate the Compiler/GOARCH fields, |
| 273 | // especially since they are relatively new (see #63700). |
| 274 | // Provide a sensible fallback in this case. |
| 275 | ld.sizes = types.SizesFor("gc", runtime.GOARCH) |
| 276 | if ld.sizes == nil { // gccgo-only arch |
| 277 | ld.sizes = types.SizesFor("gc", "amd64") |
| 278 | } |
| 279 | } else { |
| 280 | // Go list should never fail to deliver accurate size information. |
| 281 | // Reject the whole Load since the error is the same for every package. |
| 282 | return nil, fmt.Errorf("can't determine type sizes for compiler %q on GOARCH %q", |
| 283 | response.Compiler, response.Arch) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | ld.externalDriver = external |
| 288 | |
| 289 | return ld.refine(response) |
| 290 | } |
| 291 | |
| 292 | // defaultDriver is a driver that implements go/packages' fallback behavior. |
| 293 | // It will try to request to an external driver, if one exists. If there's |
searching dependent graphs…