doc.go 3.2 KB

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