go/packages: stop parsing files if the context is canceled

`stamblerre/gocode`, which is the fork of `mdempsky/gocode` that supports Go 1.11 modules, uses `packages.Load` to load all of the Go code for, among other things, IDE autocomplete support.  A common aspect to IDE autocomplete is asking for suggestions very frequently (perhaps at every character typed).  Once the user has typed another character, the previous request for autocomplete is invalid and can be canceled.

`packages.Load` already stops its `go list` component if the context is canceled, but if the context is canceled afterward, then it will parse all of the files that it found.  This change stops the parsing of Go files once it detects that the context has been canceled.  When a file has not been processed due to cancelation, its error will be set to that of the context.

This change dramatically improves the performance of the `stamblerre/gocode` fork when requests have been canceled.

Change-Id: Iba8c1e08eefa59137559ac9108238bfe5ba4ac21
GitHub-Last-Rev: 11a2210c8ce2ed9db9462ddc3e9676476f49f937
GitHub-Pull-Request: golang/tools#72
Reviewed-on: https://go-review.googlesource.com/c/159259
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/go/packages/packages.go b/go/packages/packages.go
index 5da437f..f4aac56 100644
--- a/go/packages/packages.go
+++ b/go/packages/packages.go
@@ -773,6 +773,11 @@
 	parsed := make([]*ast.File, n)
 	errors := make([]error, n)
 	for i, file := range filenames {
+		if ld.Config.Context.Err() != nil {
+			parsed[i] = nil
+			errors[i] = ld.Config.Context.Err()
+			continue
+		}
 		wg.Add(1)
 		go func(i int, filename string) {
 			ioLimit <- true // wait