• Rob Pike's avatar
    fmt.Printf: introduce notation for random access to arguments. · 7472ce0e
    Rob Pike authored
    This text is added to doc.go:
    
            Explicit argument indexes:
    
            In Printf, Sprintf, and Fprintf, the default behavior is for each
            formatting verb to format successive arguments passed in the call.
            However, the notation [n] immediately before the verb indicates that the
            nth one-indexed argument is to be formatted instead. The same notation
            before a '*' for a width or precision selects the argument index holding
            the value. After processing a bracketed expression [n], arguments n+1,
            n+2, etc. will be processed unless otherwise directed.
    
            For example,
                    fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
            will yield "22, 11", while
                    fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
            equivalent to
                    fmt.Sprintf("%6.2f", 12.0),
            will yield " 12.00". Because an explicit index affects subsequent verbs,
            this notation can be used to print the same values multiple times
            by resetting the index for the first argument to be repeated:
                    fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
            will yield "16 17 0x10 0x11".
    
    The notation chosen differs from that in C, but I believe it's easier to read
    and to remember (we're indexing the arguments), and compatibility with
    C's printf was never a strong goal anyway.
    
    While we're here, change the word "field" to "arg" or "argument" in the
    code; it was being misused and was confusing.
    
    R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
    CC=golang-dev
    https://golang.org/cl/9680043
    7472ce0e
Name
Last commit
Last update
..
articles Loading commit data...
codewalk Loading commit data...
devel Loading commit data...
gopher Loading commit data...
play Loading commit data...
progs Loading commit data...
Makefile Loading commit data...
cmd.html Loading commit data...
code.html Loading commit data...
contrib.html Loading commit data...
contribute.html Loading commit data...
debugging_with_gdb.html Loading commit data...
docs.html Loading commit data...
effective_go.html Loading commit data...
gccgo_contribute.html Loading commit data...
gccgo_install.html Loading commit data...
go-logo-black.png Loading commit data...
go-logo-blue.png Loading commit data...
go-logo-white.png Loading commit data...
go1.1.html Loading commit data...
go1.2.txt Loading commit data...
go1.html Loading commit data...
go1compat.html Loading commit data...
go_faq.html Loading commit data...
go_mem.html Loading commit data...
go_spec.html Loading commit data...
godocs.js Loading commit data...
help.html Loading commit data...
ie.css Loading commit data...
install-source.html Loading commit data...
install.html Loading commit data...
jquery.js Loading commit data...
logo-153x55.png Loading commit data...
reference.html Loading commit data...
root.html Loading commit data...
share.png Loading commit data...
sieve.gif Loading commit data...
style.css Loading commit data...
tos.html Loading commit data...