On Sunday, April 30, 2017 at 10:54:07 AM UTC+8, feilengcui008 wrote: > > So, if i do this: > //assign some var to interface type > var i io.Reader = readerImpl > i.Read(buf) > > the interface i's dynamic type infomation is filled by type deduction in > compiling phase? >
yes > > also the function table in itab field of interface implementation filling > with the actually methods of readerImpl also happened in compiling phase? > compiler can, but this is done at run time. > > if so, this is kind of similar with C++ virtual functions? > yes, I think so. > > > 在 2017年4月30日星期日 UTC+8上午3:23:54,T L写道: >> >> >> >> On Saturday, April 29, 2017 at 9:56:20 PM UTC+8, feilengcui008 wrote: >>> >>> Hey, All >>> >>> I'm curious about what happened when map or slice is assigned to >>> interface variables. >>> >>> I know the interface implementation in go runtime is: >>> type iface struct{ >>> tab *itab >>> data unsafe.Pointer >>> } >>> and the interface static and dynamic type infomation are stored in itab, >>> when we use reflect.TypeOf or reflect.ValueOf, >>> we can get the dynamic type infomation of this interface. >>> >>> But it seems that the implementations of slice or map do not have the >>> underline element type infomation, here is the map implementation: >>> // A header for a Go map. >>> type hmap struct { >>> count int // # live cells == size of map. Must be first (used by >>> len() builtin) >>> flags uint8 >>> B uint8 // log_2 of # of buckets (can hold up to loadFactor * >>> 2^B items) >>> noverflow uint16 // approximate number of overflow buckets; see >>> incrnoverflow for details >>> hash0 uint32 // hash seed >>> >>> buckets unsafe.Pointer // array of 2^B Buckets. may be nil if >>> count==0. >>> oldbuckets unsafe.Pointer // previous bucket array of half the size, >>> non-nil only when growing >>> nevacuate uintptr // progress counter for evacuation (buckets >>> less than this have been evacuated) >>> overflow *[2]*[]*bmap >>> } >>> >>> So when we use reflect.TypeOf or reflect.ValueOf, the map variable will >>> be transformed to interface{} and the empty interface will contains the >>> type information of the map, how does this happen? where does the >>> interface{} get the type information? >>> >> >> compiler knows what type the map/slice is. >> >> >>> >>> >>> Thanks in advance. >>> >> -- 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. For more options, visit https://groups.google.com/d/optout.