doc.go 2.46 KB
Newer Older
Rob Pike's avatar
Rob Pike committed
1 2 3 4 5
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
Russ Cox's avatar
Russ Cox committed
6
Gofmt formats Go programs.
Rob Pike's avatar
Rob Pike committed
7 8 9 10

Without an explicit path, it processes the standard input.  Given a file,
it operates on that file; given a directory, it operates on all .go files in
that directory, recursively.  (Files starting with a period are ignored.)
11
By default, gofmt prints the reformatted sources to standard output.
Rob Pike's avatar
Rob Pike committed
12 13 14 15 16

Usage:
	gofmt [flags] [path ...]

The flags are:
17 18 19 20
	-d
		Do not print reformatted sources to standard output.
		If a file's formatting is different than gofmt's, print diffs
		to standard output.
21 22
	-e
		Print all (including spurious) errors.
Rob Pike's avatar
Rob Pike committed
23
	-l
24 25 26
		Do not print reformatted sources to standard output.
		If a file's formatting is different from gofmt's, print its name
		to standard output.
27
	-r rule
28
		Apply the rewrite rule to the source before reformatting.
29
	-s
30
		Try to simplify code (after applying the rewrite rule, if any).
Rob Pike's avatar
Rob Pike committed
31
	-w
32 33 34
		Do not print reformatted sources to standard output.
		If a file's formatting is different from gofmt's, overwrite it
		with gofmt's version.
35 36

Formatting control flags:
37
	-comments=true
38
		Print comments; if false, all comments are elided from the output.
Rob Pike's avatar
Rob Pike committed
39
	-spaces
40
		Align with spaces instead of tabs.
41
	-tabindent
42
		Indent with tabs independent of -spaces.
Rob Pike's avatar
Rob Pike committed
43
	-tabwidth=8
44
		Tab width in spaces.
Rob Pike's avatar
Rob Pike committed
45

46

47 48 49 50 51
The rewrite rule specified with the -r flag must be a string of the form:

	pattern -> replacement

Both pattern and replacement must be valid Go expressions.
52 53
In the pattern, single-character lowercase identifiers serve as
wildcards matching arbitrary sub-expressions; those expressions
54 55
will be substituted for the same identifiers in the replacement.

56 57 58 59 60 61
When gofmt reads from standard input, it accepts either a full Go program
or a program fragment.  A program fragment must be a syntactically
valid declaration list, statement list, or expression.  When formatting
such a fragment, gofmt preserves leading indentation as well as leading
and trailing spaces, so that individual sections of a Go program can be
formatted by piping them through gofmt.
62 63 64 65 66 67 68 69 70 71 72 73 74 75

Examples

To check files for unnecessary parentheses:

	gofmt -r '(a) -> a' -l *.go

To remove the parentheses:

	gofmt -r '(a) -> a' -w *.go

To convert the package tree from explicit slice upper bounds to implicit ones:

	gofmt -r 'α[β:len(α)] -> α[β:]' -w $GOROOT/src/pkg
Rob Pike's avatar
Rob Pike committed
76 77
*/
package documentation
78 79

// BUG(rsc): The implementation of -r is a bit slow.