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