...

Source file src/pkg/cmd/gofmt/doc.go

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

View as plain text