Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
df46b334
Commit
df46b334
authored
May 08, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update tutorial text to refer to io.Reader etc.
R=rsc DELTA=15 (0 added, 5 deleted, 10 changed) OCL=28526 CL=28532
parent
fb24d792
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
15 deletions
+10
-15
go_tutorial.txt
doc/go_tutorial.txt
+10
-15
No files found.
doc/go_tutorial.txt
View file @
df46b334
...
...
@@ -632,19 +632,19 @@ be converted to an interface variable that implements the method.
Schematically, given a value "v", it does this:
type String interface {
type String
er
interface {
String() string
}
s, ok := v.(String
); // Test whether v satisfies "String
"
s, ok := v.(String
er); // Test whether v implements "String()
"
if ok {
result = s.String()
} else {
result = default_output(v)
}
The code uses a ``type assertion'' ("v.(String)") to test if the value stored in
"v" satisfies the "String" interface; if it does, "s"
The code uses a ``type assertion'' ("v.(String
er
)") to test if the value stored in
"v" satisfies the "String
er
" interface; if it does, "s"
will become an interface variable implementing the method and "ok" will
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
...
...
@@ -652,25 +652,20 @@ operations such as type conversion, map update, communications, and so on,
although this is the only appearance in this tutorial.)
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
not create any ambiguity because methods only appear in association
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)).
In this snippet the name "Stringer" follows the convention that we add "[e]r"
to interfaces describing simple method sets like this.
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
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.Write
r
", which is an
interface type defined in the "io" library:
type Write interface {
type Write
r
interface {
Write(p []byte) (n int, err *os.Error);
}
(This interface is another
doubled
name, this time for "Write"; there are also
"io.Read
", "io.ReadWrite
", and so on.)
(This interface is another
conventional
name, this time for "Write"; there are also
"io.Read
er", "io.ReadWriter
", and so on.)
Thus you can call "Fprintf" on any type that implements a standard "Write()"
method, not just files but also network channels, buffers, rot13ers, whatever
you want.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment