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