From: Rob Pike Date: Mon, 14 Jun 2010 19:27:22 +0000 (-0700) Subject: tutorial: update discussion of variadic functions X-Git-Tag: weekly.2010-06-21~35 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b9055629c191deab9c4dffc0d0b5f8fb31687e1d;p=gostls13.git tutorial: update discussion of variadic functions R=rsc CC=golang-dev https://golang.org/cl/1677042 --- diff --git a/doc/go_tutorial.html b/doc/go_tutorial.html index 40fef30693..7eb09b5b4b 100644 --- a/doc/go_tutorial.html +++ b/doc/go_tutorial.html @@ -938,14 +938,19 @@ 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.

diff --git a/doc/go_tutorial.txt b/doc/go_tutorial.txt index 76dba34efd..6ab6094c02 100644 --- a/doc/go_tutorial.txt +++ b/doc/go_tutorial.txt @@ -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.