* 'Dan Kortschak' via golang-nuts <golang-nuts@googlegroups.com> [210606 06:43]: > On Sun, 2021-06-06 at 03:17 -0700, Brian Candler wrote: > > When you assign a regular (non-pointer) value to an interface > > variable, it does take a copy of that value: > > https://play.golang.org/p/XyBREDL4BGw > > It depends on whether it's safe to leave uncopied or not. You can see > this here https://play.golang.org/p/q1729cX09BQ
"Copying an interface value makes a copy of the thing stored in the interface value. If the interface value holds a struct, copying the interface value makes a copy of the struct." I believe this is patently false. It is neither conceptually required by the language, nor is it true in practice, irregardless of any optimizations. 1. It is required (as pointed out earlier in this thread) that _assignment to_ an interface from a concrete value requires (at least conceptually) copying the value (obviously, if the value is a pointer type, the pointer, not the referent is copied). 2. It is also required (conceptually) that invoking a method on the interface value, where the method on the concrete value has a non-pointer receiver, makes a copy of the value to be used as the receiver. 3. Furthermore, the complement of the first point is that when using type assertion to obtain the concrete value from an interface, the value (pointer if it is of pointer type) must be copied. These three points, along with the fact that you cannot directly access the concrete value without type assertion, are sufficient to make it safe to copy an interface without making a copy of the concrete value held by the interface, regardless of the implementation. The statement in question should be removed from the FAQ because it is just plain wrong, as well as because it is confusing and leads new Go programmers to adopt bad programming practices. I would replace it, and the subsequent statement about an interface holding a pointer type, with a statement that says something along the lines of "Assigning a concrete value to an interface requires copying the value (if the value is a pointer type, only the pointer is copied), but copying an interface value never requires making another copy of the concrete value, even if it is not a pointer type." As an optimization (I don't know what the current compiler does), using type assertion from one interface type to another interface type does not require making a copy of the concrete value. Also, type assertion to a concrete type can use the value from the interface directly if the compiler can prove that it is not modified and doesn't escape. ...Marvin -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/20210606211250.s5tv5jcsk75ex2hu%40basil.wdw.