github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/doc/articles/godoc_documenting_go_code.html (about) 1 <!--{ 2 "Title": "Godoc: documenting Go code", 3 "Template": true 4 }--> 5 6 <p> 7 The Go project takes documentation seriously. Documentation is a huge part of 8 making software accessible and maintainable. Of course it must be well-written 9 and accurate, but it also must be easy to write and to maintain. Ideally, it 10 should be coupled to the code itself so the documentation evolves along with the 11 code. The easier it is for programmers to produce good documentation, the better 12 for everyone. 13 </p> 14 15 <p> 16 To that end, we have developed the <a href="/cmd/godoc/">godoc</a> documentation 17 tool. This article describes godoc's approach to documentation, and explains how 18 you can use our conventions and tools to write good documentation for your own 19 projects. 20 </p> 21 22 <p> 23 Godoc parses Go source code - including comments - and produces documentation as 24 HTML or plain text. The end result is documentation tightly coupled with the 25 code it documents. For example, through godoc's web interface you can navigate 26 from a function's <a href="/pkg/strings/#HasPrefix">documentation</a> to its 27 <a href="/src/pkg/strings/strings.go?#L312">implementation</a> with one click. 28 </p> 29 30 <p> 31 Godoc is conceptually related to Python's 32 <a href="http://www.python.org/dev/peps/pep-0257/">Docstring</a> and Java's 33 <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html">Javadoc</a>, 34 but its design is simpler. The comments read by godoc are not language 35 constructs (as with Docstring) nor must they have their own machine-readable 36 syntax (as with Javadoc). Godoc comments are just good comments, the sort you 37 would want to read even if godoc didn't exist. 38 </p> 39 40 <p> 41 The convention is simple: to document a type, variable, constant, function, or 42 even a package, write a regular comment directly preceding its declaration, with 43 no intervening blank line. Godoc will then present that comment as text 44 alongside the item it documents. For example, this is the documentation for the 45 <code>fmt</code> package's <a href="/pkg/fmt/#Fprint"><code>Fprint</code></a> 46 function: 47 </p> 48 49 {{code "/src/pkg/fmt/print.go" `/Fprint formats using the default/` `/func Fprint/`}} 50 51 <p> 52 Notice this comment is a complete sentence that begins with the name of the 53 element it describes. This important convention allows us to generate 54 documentation in a variety of formats, from plain text to HTML to UNIX man 55 pages, and makes it read better when tools truncate it for brevity, such as when 56 they extract the first line or sentence. 57 </p> 58 59 <p> 60 Comments on package declarations should provide general package documentation. 61 These comments can be short, like the <a href="/pkg/sort/"><code>sort</code></a> 62 package's brief description: 63 </p> 64 65 {{code "/src/pkg/sort/sort.go" `/Package sort provides/` `/package sort/`}} 66 67 <p> 68 They can also be detailed like the <a href="/pkg/encoding/gob/"><code>gob</code></a> 69 package's overview. That package uses another convention for packages 70 that need large amounts of introductory documentation: the package comment is 71 placed in its own file, <a href="/src/pkg/encoding/gob/doc.go">doc.go</a>, which 72 contains only those comments and a package clause. 73 </p> 74 75 <p> 76 When writing package comments of any size, keep in mind that their first 77 sentence will appear in godoc's <a href="/pkg/">package list</a>. 78 </p> 79 80 <p> 81 Comments that are not adjacent to a top-level declaration are omitted from 82 godoc's output, with one notable exception. Top-level comments that begin with 83 the word <code>"BUG(who)"</code> are recognized as known bugs, and included in 84 the "Bugs" section of the package documentation. The "who" part should be the 85 user name of someone who could provide more information. For example, this is a 86 known issue from the <a href="/pkg/sync/atomic/#pkg-note-BUG"><code>sync/atomic</code></a> package: 87 </p> 88 89 <pre> 90 // BUG(rsc): On x86-32, the 64-bit functions use instructions 91 // unavailable before the Pentium MMX. On both ARM and x86-32, it is the 92 // caller's responsibility to arrange for 64-bit alignment of 64-bit 93 // words accessed atomically. 94 </pre> 95 96 <p> 97 Godoc treats executable commands in the same way. It looks for a comment on 98 package main, which is sometimes put in a separate file called <code>doc.go</code>. 99 For example, see the 100 <a href="/cmd/godoc/">godoc documentation</a> and its corresponding 101 <a href="/src/cmd/godoc/doc.go">doc.go</a> file. 102 </p> 103 104 <p> 105 There are a few formatting rules that Godoc uses when converting comments to 106 HTML: 107 </p> 108 109 <ul> 110 <li> 111 Subsequent lines of text are considered part of the same paragraph; you must 112 leave a blank line to separate paragraphs. 113 </li> 114 <li> 115 Pre-formatted text must be indented relative to the surrounding comment text 116 (see gob's <a href="/src/pkg/encoding/gob/doc.go">doc.go</a> for an example). 117 </li> 118 <li> 119 URLs will be converted to HTML links; no special markup is necessary. 120 </li> 121 </ul> 122 123 <p> 124 Note that none of these rules requires you to do anything out of the ordinary. 125 </p> 126 127 <p> 128 In fact, the best thing about godoc's minimal approach is how easy it is to use. 129 As a result, a lot of Go code, including all of the standard library, already 130 follows the conventions. 131 </p> 132 133 <p> 134 Your own code can present good documentation just by having comments as 135 described above. Any Go packages installed inside <code>$GOROOT/src/pkg</code> 136 and any <code>GOPATH</code> work spaces will already be accessible via godoc's 137 command-line and HTTP interfaces, and you can specify additional paths for 138 indexing via the <code>-path</code> flag or just by running <code>"godoc ."</code> 139 in the source directory. See the <a href="/cmd/godoc/">godoc documentation</a> 140 for more details. 141 </p> 142 143 <p> 144 Godoc recognizes example functions written according to the 145 <a href="/pkg/testing/#pkg-overview"><code>testing</code></a> package's naming 146 conventions and presents them appropriately. 147 </p>