Commit 5be78668 authored by Rob Pike's avatar Rob Pike

fmt: unify the printing examples

Provide an example for each of the printing functions (Print,
Sprintf, Fprintln etc.), and make them all produce the same output
so their usage can be compared.

Also add a package-level example explaining the difference between
how Printf, Println, and Print behave.

There are more examples to come.

Update #27554.

Change-Id: Ide03e5233f3762a9ee2ac0269f534ab927562ce2
Reviewed-on: https://go-review.googlesource.com/136615Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 206fd788
...@@ -19,6 +19,7 @@ func ExampleErrorf() { ...@@ -19,6 +19,7 @@ func ExampleErrorf() {
const name, id = "bueller", 17 const name, id = "bueller", 17
err := fmt.Errorf("user %q (id %d) not found", name, id) err := fmt.Errorf("user %q (id %d) not found", name, id)
fmt.Println(err.Error()) fmt.Println(err.Error())
// Output: user "bueller" (id 17) not found // Output: user "bueller" (id 17) not found
} }
...@@ -31,7 +32,7 @@ func ExampleFscanf() { ...@@ -31,7 +32,7 @@ func ExampleFscanf() {
r := strings.NewReader("5 true gophers") r := strings.NewReader("5 true gophers")
n, err := fmt.Fscanf(r, "%d %t %s", &i, &b, &s) n, err := fmt.Fscanf(r, "%d %t %s", &i, &b, &s)
if err != nil { if err != nil {
panic(err) fmt.Fprintf(os.Stderr, "Fscanf: %v\n", err)
} }
fmt.Println(i, b, s) fmt.Println(i, b, s)
fmt.Println(n) fmt.Println(n)
...@@ -40,98 +41,168 @@ func ExampleFscanf() { ...@@ -40,98 +41,168 @@ func ExampleFscanf() {
// 3 // 3
} }
func ExampleSprintf() { func ExampleFscanln() {
i := 30 s := `dmr 1771 1.61803398875
s := "Aug" ken 271828 3.14159`
sf := fmt.Sprintf("Today is %d %s", i, s) r := strings.NewReader(s)
fmt.Println(sf) var a string
fmt.Println(len(sf)) var b int
var c float64
for {
n, err := fmt.Fscanln(r, &a, &b, &c)
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Printf("%d: %s, %d, %f\n", n, a, b, c)
}
// Output: // Output:
// Today is 30 Aug // 3: dmr, 1771, 1.618034
// 15 // 3: ken, 271828, 3.141590
} }
func ExamplePrint() { func ExamplePrint() {
n, err := fmt.Print("there", "are", 99, "gophers", "\n") const name, age = "Kim", 22
if err != nil { fmt.Print(name, " is ", age, " years old.\n")
panic(err)
} // It is conventional not to worry about any
fmt.Print(n) // error returned by Print.
// Output: // Output:
// thereare99gophers // Kim is 22 years old.
// 18
} }
func ExamplePrintln() { func ExamplePrintln() {
n, err := fmt.Println("there", "are", 99, "gophers") const name, age = "Kim", 22
if err != nil { fmt.Println(name, "is", age, "years old.")
panic(err)
} // It is conventional not to worry about any
fmt.Print(n) // error returned by Println.
// Output:
// Kim is 22 years old.
}
func ExamplePrintf() {
const name, age = "Kim", 22
fmt.Printf("%s is %d years old.\n", name, age)
// It is conventional not to worry about any
// error returned by Printf.
// Output:
// Kim is 22 years old.
}
func ExampleSprint() {
const name, age = "Kim", 22
s := fmt.Sprint(name, " is ", age, " years old.\n")
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
// Output: // Output:
// there are 99 gophers // Kim is 22 years old.
// 21
} }
func ExampleSprintln() { func ExampleSprintln() {
s := "Aug" const name, age = "Kim", 22
sl := fmt.Sprintln("Today is 30", s) s := fmt.Sprintln(name, "is", age, "years old.")
fmt.Printf("%q", sl)
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
// Output:
// Kim is 22 years old.
}
func ExampleSprintf() {
const name, age = "Kim", 22
s := fmt.Sprintf("%s is %d years old.\n", name, age)
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
// Output: // Output:
// "Today is 30 Aug\n" // Kim is 22 years old.
} }
func ExampleFprint() { func ExampleFprint() {
n, err := fmt.Fprint(os.Stdout, "there", "are", 99, "gophers", "\n") const name, age = "Kim", 22
n, err := fmt.Fprint(os.Stdout, name, " is ", age, " years old.\n")
// The n and err return values from Fprint are
// those returned by the underlying io.Writer.
if err != nil { if err != nil {
panic(err) fmt.Fprintf(os.Stderr, "Fprint: %v\n", err)
} }
fmt.Print(n) fmt.Print(n, " bytes written.\n")
// Output: // Output:
// thereare99gophers // Kim is 22 years old.
// 18 // 21 bytes written.
} }
func ExampleFprintln() { func ExampleFprintln() {
n, err := fmt.Fprintln(os.Stdout, "there", "are", 99, "gophers") const name, age = "Kim", 22
n, err := fmt.Fprintln(os.Stdout, name, "is", age, "years old.")
// The n and err return values from Fprintln are
// those returned by the underlying io.Writer.
if err != nil { if err != nil {
panic(err) fmt.Fprintf(os.Stderr, "Fprintln: %v\n", err)
} }
fmt.Print(n) fmt.Println(n, "bytes written.")
// Output: // Output:
// there are 99 gophers // Kim is 22 years old.
// 21 // 21 bytes written.
} }
func ExampleFscanln() { func ExampleFprintf() {
s := `dmr 1771 1.61803398875 const name, age = "Kim", 22
ken 271828 3.14159` n, err := fmt.Fprintf(os.Stdout, "%s is %d years old.\n", name, age)
r := strings.NewReader(s)
var a string // The n and err return values from Fprintf are
var b int // those returned by the underlying io.Writer.
var c float64 if err != nil {
for { fmt.Fprintf(os.Stderr, "Fprintf: %v\n", err)
n, err := fmt.Fscanln(r, &a, &b, &c)
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Printf("%d: %s, %d, %f\n", n, a, b, c)
} }
fmt.Printf("%d bytes written.\n", n)
// Output: // Output:
// 3: dmr, 1771, 1.618034 // Kim is 22 years old.
// 3: ken, 271828, 3.141590 // 21 bytes written.
} }
func ExampleSprint() { // Print, Println, and Printf lay out their arguments differently. In this example
s := fmt.Sprint("there", "are", "99", "gophers") // we can compare their behaviors. Println always adds blanks between the items it
fmt.Println(s) // prints, while Print adds blanks only between non-string arguments and Printf
fmt.Println(len(s)) // does exactly what it is told.
// Sprint, Sprintln, Sprintf, Fprint, Fprintln, and Fprintf behave the same as
// their corresponding Print, Println, and Printf functions shown here.
func Example_printers() {
a, b := 3.0, 4.0
h := math.Hypot(a, b)
// Print inserts blanks between arguments when neither is a string.
// It does not add a newline to the output, so we add one explicitly.
fmt.Print("The vector (", a, b, ") has length ", h, ".\n")
// Println always inserts spaces between its arguments,
// so it cannot be used to produce the same output as Print in this case;
// its output has extra spaces.
// Also, Println always adds a newline to the output.
fmt.Println("The vector (", a, b, ") has length", h, ".")
// Printf provides complete control but is more complex to use.
// It does not add a newline to the output, so we add one explicitly
// at the end of the format specifier string.
fmt.Printf("The vector (%g %g) has length %g.\n", a, b, h)
// Output: // Output:
// thereare99gophers // The vector (3 4) has length 5.
// 17 // The vector ( 3 4 ) has length 5 .
// The vector (3 4) has length 5.
} }
// These examples demonstrate the basics of printing using a format string. Printf, // These examples demonstrate the basics of printing using a format string. Printf,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment