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