% go version
go version devel +bbd1dcd Wed Jun 1 21:32:46 2016 +0000 linux/amd64
% cat ~/a.go
package main
import (
"bytes"
"io"
"reflect"
"sync"
)
func main() {
t := reflect.StructOf([]reflect.StructField{
{Type: reflect.TypeOf(sync.Mutex{}), Anonymous: true},
{Type: reflect.TypeOf(bytes.Buffer{}), Anonymous: true},
})
x := reflect.New(t).Interface()
_ = x.(io.Writer)
}
% go run ~/a.go
panic: interface conversion: *truct { sync.Mutex; bytes.Buffer } is not io.Writer: missing method Write
I expected that x would have generated wrapper methods for Mutex.Lock, etc.
Interestingly, adding this declaration inside func main (not at package level):
var _ interface{} = new(struct {
sync.Mutex
bytes.Buffer
})
causes the program to succeed. Presumably it causes the compiler to generate the correct type information, which reflect.StructOf then finds.
I expected that x would have generated wrapper methods for Mutex.Lock, etc.
Interestingly, adding this declaration inside func main (not at package level):
causes the program to succeed. Presumably it causes the compiler to generate the correct type information, which
reflect.StructOfthen finds.