import "golang.org/x/tools/go/packages" func main() { cfg := &packages.Config{ Mode: packages.LoadAllSyntax, Tests: true, } packages.Load(cfg, "./...") }
import "golang.org/x/tools/go/packages" func main() { cfg := &packages.Config{ Mode: packages.LoadAllSyntax, Tests: true, LoadTests: true, TestsUseSum: true, Dir: "./", Env: []string{"GO111MODULE=on"}, BuildFlags: []string{"-tags=foo"}, Tags: []string{"foo"}, Fset: token.NewFileSet(), TypeChecker: types.Config{Importer: importer.For("source", nil)}, Module: &packages.Module{}, Context: context.Background(), Overlay: map[string][]byte{}, TestingFlags: []string{"-v", "-race"}, } packages.ImportWithTests(cfg, "mypackage") }The above code imports the packages library and loads the package with tests for "mypackage". It sets various flags and options for the Config object, such as LoadTests, TestsUseSum, and BuildFlags, which customize how the loader loads and parses the package. It also sets the testing flags for the package.