mod, err := runCmd(append(os.Environ(), "GOWORK=off"), "go", "list", "-m")
if err == nil && mod != "" && mod != "command-line-arguments" {
// If there's a module, go to the module's doc page.
- return doPkgsite(mod)
+ return doPkgsite(mod, "")
}
gowork, err := runCmd(nil, "go", "env", "GOWORK")
if err == nil && gowork != "" {
// Outside a module, but in a workspace, go to the home page
// with links to each of the modules' pages.
- return doPkgsite("")
+ return doPkgsite("", "")
}
// Outside a module or workspace, go to the documentation for the standard library.
- return doPkgsite("std")
+ return doPkgsite("std", "")
}
// If args are provided, we need to figure out which page to open on the pkgsite
}
if found {
if serveHTTP {
- path, err := objectPath(userPath, pkg, symbol, method)
+ path, fragment, err := objectPath(userPath, pkg, symbol, method)
if err != nil {
return err
}
- return doPkgsite(path)
+ return doPkgsite(path, fragment)
}
return nil
}
return strings.TrimSpace(stdout.String()), nil
}
-func objectPath(userPath string, pkg *Package, symbol, method string) (string, error) {
+// returns a path followed by a fragment (or an error)
+func objectPath(userPath string, pkg *Package, symbol, method string) (string, string, error) {
var err error
path := pkg.build.ImportPath
if path == "." {
// go list to get the import path.
path, err = runCmd(nil, "go", "list", userPath)
if err != nil {
- return "", err
+ return "", "", err
}
}
if symbol != "" && method != "" {
object = symbol + "." + method
}
- if object != "" {
- path = path + "#" + object
- }
- return path, nil
+ return path, object, nil
}
// failMessage creates a nicely formatted error message when there is no result to show.
return port, nil
}
-func doPkgsite(urlPath string) error {
+func doPkgsite(urlPath, fragment string) error {
port, err := pickUnusedPort()
if err != nil {
return fmt.Errorf("failed to find port for documentation server: %v", err)
if err != nil {
return fmt.Errorf("internal error: failed to construct url: %v", err)
}
+ if fragment != "" {
+ path += "#" + fragment
+ }
// Turn off the default signal handler for SIGINT (and SIGQUIT on Unix)
// and instead wait for the child process to handle the signal and