github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/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  It uses tabs (width = 8) for indentation and blanks for alignment.
     8  
     9  Without an explicit path, it processes the standard input.  Given a file,
    10  it operates on that file; given a directory, it operates on all .go files in
    11  that directory, recursively.  (Files starting with a period are ignored.)
    12  By default, gofmt prints the reformatted sources to standard output.
    13  
    14  Usage:
    15  	gofmt [flags] [path ...]
    16  
    17  The flags are:
    18  	-d
    19  		Do not print reformatted sources to standard output.
    20  		If a file's formatting is different than gofmt's, print diffs
    21  		to standard output.
    22  	-e
    23  		Print all (including spurious) errors.
    24  	-l
    25  		Do not print reformatted sources to standard output.
    26  		If a file's formatting is different from gofmt's, print its name
    27  		to standard output.
    28  	-r rule
    29  		Apply the rewrite rule to the source before reformatting.
    30  	-s
    31  		Try to simplify code (after applying the rewrite rule, if any).
    32  	-w
    33  		Do not print reformatted sources to standard output.
    34  		If a file's formatting is different from gofmt's, overwrite it
    35  		with gofmt's version.
    36  
    37  Debugging support:
    38  	-cpuprofile filename
    39  		Write cpu profile to the specified file.
    40  
    41  
    42  The rewrite rule specified with the -r flag must be a string of the form:
    43  
    44  	pattern -> replacement
    45  
    46  Both pattern and replacement must be valid Go expressions.
    47  In the pattern, single-character lowercase identifiers serve as
    48  wildcards matching arbitrary sub-expressions; those expressions
    49  will be substituted for the same identifiers in the replacement.
    50  
    51  When gofmt reads from standard input, it accepts either a full Go program
    52  or a program fragment.  A program fragment must be a syntactically
    53  valid declaration list, statement list, or expression.  When formatting
    54  such a fragment, gofmt preserves leading indentation as well as leading
    55  and trailing spaces, so that individual sections of a Go program can be
    56  formatted by piping them through gofmt.
    57  
    58  Examples
    59  
    60  To check files for unnecessary parentheses:
    61  
    62  	gofmt -r '(a) -> a' -l *.go
    63  
    64  To remove the parentheses:
    65  
    66  	gofmt -r '(a) -> a' -w *.go
    67  
    68  To convert the package tree from explicit slice upper bounds to implicit ones:
    69  
    70  	gofmt -r 'α[β:len(α)] -> α[β:]' -w $GOROOT/src
    71  
    72  The simplify command
    73  
    74  When invoked with -s gofmt will make the following source transformations where possible.
    75  
    76  	An array, slice, or map composite literal of the form:
    77  		[]T{T{}, T{}}
    78  	will be simplified to:
    79  		[]T{{}, {}}
    80  
    81  	A slice expression of the form:
    82  		s[a:len(s)]
    83  	will be simplified to:
    84  		s[a:]
    85  
    86  	A range of the form:
    87  		for x, _ = range v {...}
    88  	will be simplified to:
    89  		for x = range v {...}
    90  */
    91  package main
    92  
    93  // BUG(rsc): The implementation of -r is a bit slow.