github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/doc/doc.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package doc implements the “go doc” command.
     6  package doc
     7  
     8  import (
     9  	"context"
    10  
    11  	"github.com/go-asm/go/cmd/go/base"
    12  	"github.com/go-asm/go/cmd/go/cfg"
    13  )
    14  
    15  var CmdDoc = &base.Command{
    16  	Run:         runDoc,
    17  	UsageLine:   "go doc [doc flags] [package|[package.]symbol[.methodOrField]]",
    18  	CustomFlags: true,
    19  	Short:       "show documentation for package or symbol",
    20  	Long: `
    21  Doc prints the documentation comments associated with the item identified by its
    22  arguments (a package, const, func, type, var, method, or struct field)
    23  followed by a one-line summary of each of the first-level items "under"
    24  that item (package-level declarations for a package, methods for a type,
    25  etc.).
    26  
    27  Doc accepts zero, one, or two arguments.
    28  
    29  Given no arguments, that is, when run as
    30  
    31  	go doc
    32  
    33  it prints the package documentation for the package in the current directory.
    34  If the package is a command (package main), the exported symbols of the package
    35  are elided from the presentation unless the -cmd flag is provided.
    36  
    37  When run with one argument, the argument is treated as a Go-syntax-like
    38  representation of the item to be documented. What the argument selects depends
    39  on what is installed in GOROOT and GOPATH, as well as the form of the argument,
    40  which is schematically one of these:
    41  
    42  	go doc <pkg>
    43  	go doc <sym>[.<methodOrField>]
    44  	go doc [<pkg>.]<sym>[.<methodOrField>]
    45  	go doc [<pkg>.][<sym>.]<methodOrField>
    46  
    47  The first item in this list matched by the argument is the one whose documentation
    48  is printed. (See the examples below.) However, if the argument starts with a capital
    49  letter it is assumed to identify a symbol or method in the current directory.
    50  
    51  For packages, the order of scanning is determined lexically in breadth-first order.
    52  That is, the package presented is the one that matches the search and is nearest
    53  the root and lexically first at its level of the hierarchy. The GOROOT tree is
    54  always scanned in its entirety before GOPATH.
    55  
    56  If there is no package specified or matched, the package in the current
    57  directory is selected, so "go doc Foo" shows the documentation for symbol Foo in
    58  the current package.
    59  
    60  The package path must be either a qualified path or a proper suffix of a
    61  path. The go tool's usual package mechanism does not apply: package path
    62  elements like . and ... are not implemented by go doc.
    63  
    64  When run with two arguments, the first is a package path (full path or suffix),
    65  and the second is a symbol, or symbol with method or struct field:
    66  
    67  	go doc <pkg> <sym>[.<methodOrField>]
    68  
    69  In all forms, when matching symbols, lower-case letters in the argument match
    70  either case but upper-case letters match exactly. This means that there may be
    71  multiple matches of a lower-case argument in a package if different symbols have
    72  different cases. If this occurs, documentation for all matches is printed.
    73  
    74  Examples:
    75  	go doc
    76  		Show documentation for current package.
    77  	go doc Foo
    78  		Show documentation for Foo in the current package.
    79  		(Foo starts with a capital letter so it cannot match
    80  		a package path.)
    81  	go doc encoding/json
    82  		Show documentation for the encoding/json package.
    83  	go doc json
    84  		Shorthand for encoding/json.
    85  	go doc json.Number (or go doc json.number)
    86  		Show documentation and method summary for json.Number.
    87  	go doc json.Number.Int64 (or go doc json.number.int64)
    88  		Show documentation for json.Number's Int64 method.
    89  	go doc cmd/doc
    90  		Show package docs for the doc command.
    91  	go doc -cmd cmd/doc
    92  		Show package docs and exported symbols within the doc command.
    93  	go doc template.new
    94  		Show documentation for html/template's New function.
    95  		(html/template is lexically before text/template)
    96  	go doc text/template.new # One argument
    97  		Show documentation for text/template's New function.
    98  	go doc text/template new # Two arguments
    99  		Show documentation for text/template's New function.
   100  
   101  	At least in the current tree, these invocations all print the
   102  	documentation for json.Decoder's Decode method:
   103  
   104  	go doc json.Decoder.Decode
   105  	go doc json.decoder.decode
   106  	go doc json.decode
   107  	cd go/src/encoding/json; go doc decode
   108  
   109  Flags:
   110  	-all
   111  		Show all the documentation for the package.
   112  	-c
   113  		Respect case when matching symbols.
   114  	-cmd
   115  		Treat a command (package main) like a regular package.
   116  		Otherwise package main's exported symbols are hidden
   117  		when showing the package's top-level documentation.
   118  	-short
   119  		One-line representation for each symbol.
   120  	-src
   121  		Show the full source code for the symbol. This will
   122  		display the full Go source of its declaration and
   123  		definition, such as a function definition (including
   124  		the body), type declaration or enclosing const
   125  		block. The output may therefore include unexported
   126  		details.
   127  	-u
   128  		Show documentation for unexported as well as exported
   129  		symbols, methods, and fields.
   130  `,
   131  }
   132  
   133  func runDoc(ctx context.Context, cmd *base.Command, args []string) {
   134  	base.Run(cfg.BuildToolexec, base.Tool("doc"), args)
   135  }