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
b9055629
Commit
b9055629
authored
Jun 14, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tutorial: update discussion of variadic functions
R=rsc CC=golang-dev
https://golang.org/cl/1677042
parent
d5a80d0b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
15 deletions
+25
-15
go_tutorial.html
doc/go_tutorial.html
+13
-8
go_tutorial.txt
doc/go_tutorial.txt
+12
-7
No files found.
doc/go_tutorial.html
View file @
b9055629
...
...
@@ -938,14 +938,19 @@ implements <code>Printf</code>, <code>Fprintf</code>, and so on.
Within the
<code>
fmt
</code>
package,
<code>
Printf
</code>
is declared with this signature:
<p>
<pre>
Printf(format string, v ...) (n int, errno os.Error)
</pre>
<p>
That
<code>
...
</code>
represents the variadic argument list that in C would
be handled using the
<code>
stdarg.h
</code>
macros but in Go is passed using
an empty interface variable (
<code>
interface {}
</code>
) and then unpacked
using the reflection library. It's off topic here but the use of
reflection helps explain some of the nice properties of Go's
<code>
Printf
</code>
,
Printf(format string, v ...interface{}) (n int, errno os.Error)
</pre>
<p>
The token
<code>
...
</code>
introduces a variable-length argument list that in C would
be handled using the
<code>
stdarg.h
</code>
macros.
In Go, variadic functions are passed a slice of the arguments of the
specified type. In
<code>
Printf
</code>
's case, the declaration says
<code>
...interface{}
</code>
so the actual type is a slice of empty interface values,
<code>
[]interface{}
</code>
.
<code>
Printf
</code>
can examine the arguments by iterating over the slice
and, for each element, using a type switch or the reflection library
to interpret the value.
It's off topic here but such run-time type analysis
helps explain some of the nice properties of Go's
<code>
Printf
</code>
,
due to the ability of
<code>
Printf
</code>
to discover the type of its arguments
dynamically.
<p>
...
...
doc/go_tutorial.txt
View file @
b9055629
...
...
@@ -622,13 +622,18 @@ We've seen simple uses of the package "fmt", which
implements "Printf", "Fprintf", and so on.
Within the "fmt" package, "Printf" is declared with this signature:
Printf(format string, v ...) (n int, errno os.Error)
That "..." represents the variadic argument list that in C would
be handled using the "stdarg.h" macros but in Go is passed using
an empty interface variable ("interface {}") and then unpacked
using the reflection library. It's off topic here but the use of
reflection helps explain some of the nice properties of Go's "Printf",
Printf(format string, v ...interface{}) (n int, errno os.Error)
The token "..." introduces a variable-length argument list that in C would
be handled using the "stdarg.h" macros.
In Go, variadic functions are passed a slice of the arguments of the
specified type. In "Printf"'s case, the declaration says "...interface{}"
so the actual type is a slice of empty interface values, "[]interface{}".
"Printf" can examine the arguments by iterating over the slice
and, for each element, using a type switch or the reflection library
to interpret the value.
It's off topic here but such run-time type analysis
helps explain some of the nice properties of Go's "Printf",
due to the ability of "Printf" to discover the type of its arguments
dynamically.
...
...
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