diff options
| author | acehinnnqru <acehinnnqru@gmail.com> | 2026-04-08 23:07:16 +0800 |
|---|---|---|
| committer | Michael Matloob <matloob@google.com> | 2026-04-09 07:51:01 -0700 |
| commit | e122bcdc9d567c21a72ba5851488edbd073d7bfe (patch) | |
| tree | c93d79bc2000ecf11424a5c01dac0c0122c62f5f | |
| parent | c4cb9a90f6b5bf0dbe8b8fe3bf994c0c50c08aaf (diff) | |
| download | go-e122bcdc9d567c21a72ba5851488edbd073d7bfe.tar.xz | |
cmd/go: respect -short flag when documenting individual symbols
The -short flag was ignored when documenting individual symbols (functions,
types, or variables) with "go doc -short package.symbol". This change adds
short flag checks to symbolDoc, typeDoc, and valueDoc functions.
Fixes #77192
Change-Id: I66935ceb5ffb5836dbd3a2fd2c7a33dd31e82001
Reviewed-on: https://go-review.googlesource.com/c/go/+/764101
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
| -rw-r--r-- | src/cmd/go/internal/doc/pkg.go | 17 | ||||
| -rw-r--r-- | src/cmd/go/testdata/script/mod_doc.txt | 14 |
2 files changed, 29 insertions, 2 deletions
diff --git a/src/cmd/go/internal/doc/pkg.go b/src/cmd/go/internal/doc/pkg.go index bd4a2f6e4f..e35ad38ebc 100644 --- a/src/cmd/go/internal/doc/pkg.go +++ b/src/cmd/go/internal/doc/pkg.go @@ -808,9 +808,14 @@ func (pkg *Package) symbolDoc(symbol string) bool { for _, fun := range pkg.findFuncs(symbol) { // Symbol is a function. decl := fun.Decl + found = true + if short { + pkg.Printf("%s\n", pkg.oneLineNode(decl)) + pkg.exampleSummary(fun.Examples, false) + continue + } pkg.emit(fun.Doc, decl) pkg.exampleSummary(fun.Examples, true) - found = true } for _, ex := range pkg.findExamples(symbol) { pkg.emitExample(ex) @@ -886,8 +891,12 @@ func (pkg *Package) valueDoc(value *doc.Value, printed map[*ast.GenDecl]bool) { return } value.Decl.Specs = specs - pkg.emit(value.Doc, value.Decl) printed[value.Decl] = true + if short { + pkg.Printf("%s\n", pkg.oneLineNode(value.Decl)) + return + } + pkg.emit(value.Doc, value.Decl) } // typeDoc prints the docs for a type, including constructors and other items @@ -900,6 +909,10 @@ func (pkg *Package) typeDoc(typ *doc.Type) { if len(decl.Specs) > 1 { decl.Specs = []ast.Spec{spec} } + if short { + pkg.Printf("%s\n", pkg.oneLineNode(spec)) + return + } pkg.emit(typ.Doc, decl) pkg.newlines(2) // Show associated methods, constants, etc. diff --git a/src/cmd/go/testdata/script/mod_doc.txt b/src/cmd/go/testdata/script/mod_doc.txt index cbfd9336dd..32edb4852a 100644 --- a/src/cmd/go/testdata/script/mod_doc.txt +++ b/src/cmd/go/testdata/script/mod_doc.txt @@ -8,6 +8,13 @@ env GOFLAGS=-mod=mod go doc y stdout 'Package y is.*alphabet' stdout 'import "x/y"' +go doc -short y.Yell +stdout '^func Yell\(\) string$' +! stdout 'Yell returns the final letter' +go doc -short y.Letter +stdout '^const Letter = "z"$' +go doc -short y.Alphabet +stdout '^type Alphabet struct\{\}$' go doc x/y stdout 'Package y is.*alphabet' ! go doc quote.Hello @@ -75,6 +82,13 @@ require rsc.io/quote v1.5.2 // Package y is the next to last package of the alphabet. package y +// Yell returns the final letter of the alphabet. +func Yell() string { return "z" } + +const Letter = "z" + +type Alphabet struct{} + -- x.go -- package x |
