diff options
| author | Rob Pike <r@golang.org> | 2010-06-14 12:27:22 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2010-06-14 12:27:22 -0700 |
| commit | 7cb69730545e4ba9295f5c8ac2dfbff18de053c1 (patch) | |
| tree | f94b78a05af88530049368a47ab7d35c9e4c9007 /doc/go_tutorial.html | |
| parent | d3c7888ab5c09725d4b90fddfcd0df0cb63106c6 (diff) | |
| download | golang-7cb69730545e4ba9295f5c8ac2dfbff18de053c1.tar.gz | |
tutorial: update discussion of variadic functions
R=rsc
CC=golang-dev
http://codereview.appspot.com/1677042
Diffstat (limited to 'doc/go_tutorial.html')
| -rw-r--r-- | doc/go_tutorial.html | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/doc/go_tutorial.html b/doc/go_tutorial.html index 40fef3069..7eb09b5b4 100644 --- a/doc/go_tutorial.html +++ b/doc/go_tutorial.html @@ -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> |
