Package fmt
import "fmt"
Package fmt implements formatted I/O with functions analogous to C's printf. The format 'verbs' are derived from C's but are simpler.
The verbs:
General:
%v the value in a default format. when printing structs, the plus flag (%+v) adds field names %#v a Go-syntax representation of the value %T a Go-syntax representation of the type of the value
Boolean:
%t the word true or false
Integer:
%b base 2 %c the character represented by the corresponding Unicode code point %d base 10 %o base 8 %x base 16, with lower-case letters for a-f %X base 16, with upper-case letters for A-F
Floating-point:
%e scientific notation, e.g. -1234.456e+78 %E scientific notation, e.g. -1234.456E+78 %f decimal point but no exponent, e.g. 123.456 %g whichever of %e or %f produces more compact output %G whichever of %E or %f produces more compact output
String and slice of bytes:
%s the uninterpreted bytes of the string or slice %q a double-quoted string safely escaped with Go syntax %x base 16 notation with two characters per byte
Pointer:
%p base 16 notation, with leading 0x
There is no 'u' flag. Integers are printed unsigned if they have unsigned type. Similarly, there is no need to specify the size of the operand (int8, int64).
For numeric values, the width and precision flags control formatting; width sets the width of the field, precision the number of places after the decimal, if appropriate. The format %6.2f prints 123.45. The width of a field is the number of Unicode code points in the string. This differs from C's printf where the field width is the number of bytes.
Other flags:
+ always print a sign for numeric values - pad with spaces on the right rather than the left (left-justify the field) # alternate format: add leading 0 for octal (%#o), 0x for hex (%#x); suppress 0x for %p (%#p); print a raw (backquoted) string if possible for %q (%#q) ' ' (space) leave a space for elided sign in numbers (% d); put spaces between bytes printing strings or slices in hex (% x) 0 pad with leading zeros rather than spaces
For each Printf-like function, there is also a Print function that takes no format and is equivalent to saying %v for every operand. Another variant Println inserts blanks between operands and appends a newline.
Regardless of the verb, if an operand is an interface value, the internal concrete value is used, not the interface itself. Thus:
var i interface{} = 23;
fmt.Printf("%v\n", i);
will print 23.
If an operand implements interface Formatter, that interface can be used for fine control of formatting.
If an operand implements method String() string that method will be used for %v, %s, or Print etc.
Package files
format.go print.gofunc Fprint
func Fprint(w io.Writer, a ...interface{}) (n int, error os.Error)
Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string.
func Fprintf
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, error os.Error)
Fprintf formats according to a format specifier and writes to w.
func Fprintln
func Fprintln(w io.Writer, a ...interface{}) (n int, error os.Error)
Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended.
func Print
func Print(a ...interface{}) (n int, errno os.Error)
Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string.
func Printf
func Printf(format string, a ...interface{}) (n int, errno os.Error)
Printf formats according to a format specifier and writes to standard output.
func Println
func Println(a ...interface{}) (n int, errno os.Error)
Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended.
func Sprint
func Sprint(a ...interface{}) string
Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.
func Sprintf
func Sprintf(format string, a ...interface{}) string
Sprintf formats according to a format specifier and returns the resulting string.
func Sprintln
func Sprintln(a ...interface{}) string
Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.
type Formatter
Formatter is the interface implemented by values with a custom formatter. The implementation of Format may call Sprintf or Fprintf(f) etc. to generate its output.
type Formatter interface {
Format(f State, c int)
}
type GoStringer
GoStringer is implemented by any value that has a GoString() method, which defines the Go syntax for that value. The GoString method is used to print values passed as an operand to a %#v format.
type GoStringer interface {
GoString() string
}
type State
State represents the printer state passed to custom formatters. It provides access to the io.Writer interface plus information about the flags and options for the operand's format specifier.
type State interface {
// Write is the function to call to emit formatted output to be printed.
Write(b []byte) (ret int, err os.Error)
// Width returns the value of the width option and whether it has been set.
Width() (wid int, ok bool)
// Precision returns the value of the precision option and whether it has been set.
Precision() (prec int, ok bool)
// Flag returns whether the flag c, a character, has been set.
Flag(int) bool
}
type Stringer
Stringer is implemented by any value that has a String method(), which defines the “native” format for that value. The String method is used to print values passed as an operand to a %s or %v format or to an unformatted printer such as Print.
type Stringer interface {
String() string
}
