allow copy of struct containing unexported fields

An experiment: allow structs to be copied even if they
contain unexported fields.  This gives packages the
ability to return opaque values in their APIs, like reflect
does for reflect.Value but without the kludgy hacks reflect
resorts to.

In general, we trust programmers not to do silly things
like *x = *y on a package's struct pointers, just as we trust
programmers not to do unicode.Letter = unicode.Digit,
but packages that want a harder guarantee can introduce
an extra level of indirection, like in the changes to os.File
in this CL or by using an interface type.

All in one CL so that it can be rolled back more easily if
we decide this is a bad idea.

Originally discussed in March 2011.
https://groups.google.com/group/golang-dev/t/3f5d30938c7c45ef

R=golang-dev, adg, dvyukov, r, bradfitz, jan.mercl, gri
CC=golang-dev
https://golang.org/cl/5372095
This commit is contained in:
Russ Cox 2011-11-15 12:20:59 -05:00
parent 0ed5e6a2be
commit d03611f628
15 changed files with 56 additions and 221 deletions

View file

@ -1,27 +0,0 @@
// errchk $G $D/$F.go
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Issue 1387
package foo
import "bytes"
func i() {
a := make([]bytes.Buffer, 1)
b := a[0] // ERROR "unexported field"
}
func f() {
a := make([]bytes.Buffer, 1)
a = append(a, a...) // ERROR "unexported field"
}
func g() {
a := make([]bytes.Buffer, 1)
b := make([]bytes.Buffer, 1)
copy(b, a) // ERROR "unexported field"
}