github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/gofmt/doc.go (about)

     1  // Copyright 2009 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  /*
     6  Gofmt formats Go programs.
     7  
     8  Without an explicit path, it processes the standard input.  Given a file,
     9  it operates on that file; given a directory, it operates on all .go files in
    10  that directory, recursively.  (Files starting with a period are ignored.)
    11  By default, gofmt prints the reformatted sources to standard output.
    12  
    13  Usage:
    14  	gofmt [flags] [path ...]
    15  
    16  The flags are:
    17  	-d
    18  		Do not print reformatted sources to standard output.
    19  		If a file's formatting is different than gofmt's, print diffs
    20  		to standard output.
    21  	-e
    22  		Print all (including spurious) errors.
    23  	-l
    24  		Do not print reformatted sources to standard output.
    25  		If a file's formatting is different from gofmt's, print its name
    26  		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).
    31  	-w
    32  		Do not print reformatted sources to standard output.
    33  		If a file's formatting is different from gofmt's, overwrite it
    34  		with gofmt's version.
    35  
    36  Formatting control flags:
    37  	-comments=true
    38  		Print comments; if false, all comments are elided from the output.
    39  	-tabs=true
    40  		Indent with tabs; if false, spaces are used instead.
    41  	-tabwidth=8
    42  		Tab width in spaces.
    43  
    44  
    45  The rewrite rule specified with the -r flag must be a string of the form:
    46  
    47  	pattern -> replacement
    48  
    49  Both pattern and replacement must be valid Go expressions.
    50  In the pattern, single-character lowercase identifiers serve as
    51  wildcards matching arbitrary sub-expressions; those expressions
    52  will be substituted for the same identifiers in the replacement.
    53  
    54  When gofmt reads from standard input, it accepts either a full Go program
    55  or a program fragment.  A program fragment must be a syntactically
    56  valid declaration list, statement list, or expression.  When formatting
    57  such a fragment, gofmt preserves leading indentation as well as leading
    58  and trailing spaces, so that individual sections of a Go program can be
    59  formatted by piping them through gofmt.
    60  
    61  Examples
    62  
    63  To check files for unnecessary parentheses:
    64  
    65  	gofmt -r '(a) -> a' -l *.go
    66  
    67  To remove the parentheses:
    68  
    69  	gofmt -r '(a) -> a' -w *.go
    70  
    71  To convert the package tree from explicit slice upper bounds to implicit ones:
    72  
    73  	gofmt -r 'α[β:len(α)] -> α[β:]' -w $GOROOT/src/pkg
    74  */
    75  package main
    76  
    77  // BUG(rsc): The implementation of -r is a bit slow.