diff options
author | Andrew Gerrand <adg@golang.org> | 2011-06-29 14:58:01 +1000 |
---|---|---|
committer | Andrew Gerrand <adg@golang.org> | 2011-06-29 14:58:01 +1000 |
commit | 01a1c9169686deb54df6b73ee8dcdb1a5cb88634 (patch) | |
tree | 2d1296ffd4bb14a3b5c3043751f67f26a87dc6fe | |
parent | dbdc8698df462a3ca78f7aaeb47310b331cb2033 (diff) | |
download | go-01a1c9169686deb54df6b73ee8dcdb1a5cb88634.tar.gz go-01a1c9169686deb54df6b73ee8dcdb1a5cb88634.zip |
[release-branch.r58] doc/faq: add question about converting from []T to []interface{}
««« CL 4639046 / 995095e59d58
doc/faq: add question about converting from []T to []interface{}
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/4639046
»»»
R=r
CC=golang-dev
https://golang.org/cl/4630077
-rw-r--r-- | doc/go_faq.html | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/doc/go_faq.html b/doc/go_faq.html index 953092f051..ef70033ace 100644 --- a/doc/go_faq.html +++ b/doc/go_faq.html @@ -598,6 +598,24 @@ the interface idea. Sometimes, though, they're necessary to resolve ambiguities among similar interfaces. </p> +<h3 id="convert_slice_of_interface"> +Can I convert a []T to an []interface{}?</h3> + +<p> +Not directly because they do not have the same representation in memory. +It is necessary to copy the elements individually to the destination +slice. This example converts a slice of <code>int</code> to a slice of +<code>interface{}</code>: +</p> + +<pre> +t := []int{1, 2, 3, 4} +s := make([]interface{}, len(t)) +for i, v := range t { + s[i] = v +} +</pre> + <h2 id="values">Values</h2> <h3 id="conversions"> |