July 2019
Intermediate to advanced
502 pages
14h
English
One of the most important helper functions is RunService(). Microservices often depend on other microservices. When testing a service, the test code often needs to run the dependent services. Here, the code builds a Go service in its target directory and executes it:
// Build and run a service in a target directoryfunc RunService(ctx context.Context, targetDir string, service string) { // Save and restore later current working dir wd, err := os.Getwd() Check(err) defer os.Chdir(wd) // Build the server if needed os.Chdir(targetDir) _, err = os.Stat("./" + service) if os.IsNotExist(err) { _, err := exec.Command("go", "build", ".").CombinedOutput() Check(err) } cmd := exec.CommandContext(ctx, "./"+service) err = cmd.Start() ...