]> Cypherpunks repositories - gostls13.git/commitdiff
doc: add FAQ: why no conversion from []T1 to []T2?
authorIan Lance Taylor <iant@golang.org>
Wed, 16 Nov 2016 19:23:53 +0000 (11:23 -0800)
committerIan Lance Taylor <iant@golang.org>
Thu, 17 Nov 2016 00:18:12 +0000 (00:18 +0000)
Fixes #16934.

Change-Id: I725704e4c4aae7023fd89edc42af7ba0d242fec8
Reviewed-on: https://go-review.googlesource.com/33327
Reviewed-by: Rob Pike <r@golang.org>
doc/go_faq.html

index 37a31ae289aaeeaa7fb9e058cd79669e259c47c5..884d98ba6e8241b1d4e921ee8a4062abbc9006c5 100644 (file)
@@ -769,6 +769,29 @@ for i, v := range t {
 }
 </pre>
 
+<h3 id="convert_slice_with_same_underlying_type">
+Can I convert []T1 to []T2 if T1 and T2 have the same underlying type?</h3>
+
+This last line of this code sample does not compile.
+
+<pre>
+type T1 int
+type T2 int
+var t1 T1
+var x = T2(t1) // OK
+var st1 []T1
+var sx = ([]T2)(st1) // NOT OK
+</pre>
+
+<p>
+In Go, types are closely tied to methods, in that every named type has
+a (possibly empty) method set.
+The general rule is that you can change the name of the type being
+converted (and thus possibly change its method set) but you can't
+change the name (and method set) of elements of a composite type.
+Go requires you to be explicit about type conversions.
+</p>
+
 <h3 id="nil_error">
 Why is my nil error value not equal to nil?
 </h3>