Documentation ¶
Overview ¶
Package prelude provides convenient functionality for Go one-liners.
Most of its source code is embedded directly in the generated source as a Prelude. It is however also used for godoc, and can be used by the golf command.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Filename is the current filename. Filename string // LineNum is the current line number, 1-based. LineNum int // Line is the current line. It may be edited by the script. // Its contents are automatically printed in -p mode. Line string // Fields is the Split field slice. See the convenience Field accessor. // Updated automatically in -a mode. Fields []string // IFS is the input field separator used in -a mode. Overridden by -F. IFS = " " // OFS is the output field separator used by Field(0). OFS = " " // Warnings controls whether to print warnings. Overridden by -w. Warnings = false // GolfFlgL controls whether to strip/add newlines on I/O. Overridden by -l. GolfFlgL = false // GolfInPlace reports whether we are in-place edit mode. // It is set when -i or -I were given on the command-line. GolfInPlace = false // GolfInPlaceBak is the file pattern for in-place edit backups. GolfInPlaceBak string // CurOut is the default writer for Print and Printf. // Overridden to each Filename in -i. CurOut io.WriteCloser = os.Stdout )
var ( // Join is an alias for strings.Join. Join = strings.Join // RE is an alias for regexp.MustCompile. RE = regexp.MustCompile )
Functions ¶
func BackupName ¶
BackupName returns the filename used as a backup in in-place edit mode.
Replacement rules follow Perl -i:
- if ext contains no '*' characters, it is appended to orig as a suffix.
- otherwise, each * is replaced with orig.
func Die ¶
func Die(xs ...interface{})
Die prints an error to stderr and exits the program with a failure status.
Arguments follow the semantics of Warn.
func Field ¶
Field retrieves a split field. Index 0 returns the entire line re-joined using the OFS. Positive values are taken to be a 1-based index to Fields. Negative values index from the end (so -1 is the last Fields element). Indexes out of range silently return the empty string.
func GAtoi ¶
GAtoi calls strconv.Atoi on s, and issues an optional warning if that returned an error.
func GSplit ¶
GSplit splits an input string, with some golf affordances. It is a bit more similar to Perl's split than it is to strings.Split.
When sep is a single space, strings.Fields is used. This is the default behavior with -a and no -F override, and operates how awk/perl do by default: The input is trimmed of leading and trailing whitespace, then any amount of (any) whitespace is taken as a field separator.
When sep has the form /pat/, pat is compiled into a regexp and regexp.Split is used.
Otherwise, sep is taken as a literal for strings.Split.
func Print ¶
func Print(xs ...interface{})
Print prints a string to CurOut.
With no arguments, the string to be printed defaults to Line.
In -l mode, a newline is appended to the string.
In -i mode, the "current output" is the replacement for the current Filename. Otherwise, it is os.Stdout.
Types ¶
This section is empty.