mirror of
https://github.com/golang/go
synced 2024-11-25 14:07:56 -07:00
update tutorial text to refer to io.Reader etc.
R=rsc DELTA=15 (0 added, 5 deleted, 10 changed) OCL=28526 CL=28532
This commit is contained in:
parent
fb24d792da
commit
df46b3342c
@ -632,19 +632,19 @@ be converted to an interface variable that implements the method.
|
|||||||
Schematically, given a value "v", it does this:
|
Schematically, given a value "v", it does this:
|
||||||
|
|
||||||
|
|
||||||
type String interface {
|
type Stringer interface {
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
s, ok := v.(String); // Test whether v satisfies "String"
|
s, ok := v.(Stringer); // Test whether v implements "String()"
|
||||||
if ok {
|
if ok {
|
||||||
result = s.String()
|
result = s.String()
|
||||||
} else {
|
} else {
|
||||||
result = default_output(v)
|
result = default_output(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
The code uses a ``type assertion'' ("v.(String)") to test if the value stored in
|
The code uses a ``type assertion'' ("v.(Stringer)") to test if the value stored in
|
||||||
"v" satisfies the "String" interface; if it does, "s"
|
"v" satisfies the "Stringer" interface; if it does, "s"
|
||||||
will become an interface variable implementing the method and "ok" will
|
will become an interface variable implementing the method and "ok" will
|
||||||
be "true". We then use the interface variable to call the method.
|
be "true". We then use the interface variable to call the method.
|
||||||
(The ''comma, ok'' pattern is a Go idiom used to test the success of
|
(The ''comma, ok'' pattern is a Go idiom used to test the success of
|
||||||
@ -652,25 +652,20 @@ operations such as type conversion, map update, communications, and so on,
|
|||||||
although this is the only appearance in this tutorial.)
|
although this is the only appearance in this tutorial.)
|
||||||
If the value does not satisfy the interface, "ok" will be false.
|
If the value does not satisfy the interface, "ok" will be false.
|
||||||
|
|
||||||
In this snippet "String" is used as both a type name and a method name. This does
|
In this snippet the name "Stringer" follows the convention that we add "[e]r"
|
||||||
not create any ambiguity because methods only appear in association
|
to interfaces describing simple method sets like this.
|
||||||
with a variable ("s.String()"); a method name can never appear in a context
|
|
||||||
where a type name is legal and vice versa. Another way to say this is that the
|
|
||||||
method "String" is only available within the scope bound to a variable of type
|
|
||||||
"String". We double-use the name because it makes the interface type
|
|
||||||
self-describing ("String" (the interface) implements "String" (the method)).
|
|
||||||
|
|
||||||
One last wrinkle. To complete the suite, besides "Printf" etc. and "Sprintf"
|
One last wrinkle. To complete the suite, besides "Printf" etc. and "Sprintf"
|
||||||
etc., there are also "Fprintf" etc. Unlike in C, "Fprintf"'s first argument is
|
etc., there are also "Fprintf" etc. Unlike in C, "Fprintf"'s first argument is
|
||||||
not a file. Instead, it is a variable of type "io.Write", which is an
|
not a file. Instead, it is a variable of type "io.Writer", which is an
|
||||||
interface type defined in the "io" library:
|
interface type defined in the "io" library:
|
||||||
|
|
||||||
type Write interface {
|
type Writer interface {
|
||||||
Write(p []byte) (n int, err *os.Error);
|
Write(p []byte) (n int, err *os.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
(This interface is another doubled name, this time for "Write"; there are also
|
(This interface is another conventional name, this time for "Write"; there are also
|
||||||
"io.Read", "io.ReadWrite", and so on.)
|
"io.Reader", "io.ReadWriter", and so on.)
|
||||||
Thus you can call "Fprintf" on any type that implements a standard "Write()"
|
Thus you can call "Fprintf" on any type that implements a standard "Write()"
|
||||||
method, not just files but also network channels, buffers, rot13ers, whatever
|
method, not just files but also network channels, buffers, rot13ers, whatever
|
||||||
you want.
|
you want.
|
||||||
|
Loading…
Reference in New Issue
Block a user