FAQ
Hi!
I've read a lot of papers and documentation, but I didnt find any
information related to the following problem.

Could somebody explain me how the memory model according to interfaces
works in go?
Lets consider the following code:

package main
import (
"fmt"
)
type A struct {
val_a int
}
func (a *A) f() int {
return a.val_a
}
type B struct {
val_b int
}
func (b *B) f() int {
return b.val_b
}
type Ifable interface{
f() int
}
func test(x Ifable){
fmt.Println(x.f())
}
func main() {
arr := []Ifable{&A{1}, &B{2}}
for _,el := range arr{
test(el)
}
}


The structs A and B have got some memory layout - they can have different
"methods", different fields etc.
The Go's compiler knows at compilation time the memory layout of A and B,
but when there are many instances of A and B, like in array 'arr', the
compiler does NOT know the memory layout of the elements during compilation
phase,
so when executing 'test(el)', the compiler have to somehow get information
where the appropriate function is placed in memory - how is it done?
Does Go use reflection internally and find such information at runtime or
it uses some other solution?

I would like to deeply understand the underlying mechanism.

Thank you very much,
Wojtek

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dan Kortschak at Jun 3, 2013 at 1:17 pm

    On Mon, 2013-06-03 at 06:03 -0700, wojtek.danilo@gmail.com wrote:
    I would like to deeply understand the underlying mechanism.
    This may help: http://research.swtch.com/interfaces

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 3, '13 at 1:03p
activeJun 3, '13 at 1:17p
posts2
users2
websitegolang.org

2 users in discussion

Wojtek Danilo: 1 post Dan Kortschak: 1 post

People

Translate

site design / logo © 2021 Grokbase