go - Statically typed definitions vs dynamic lookup -


on book the go programming language phrasebook said:

if require performance, can use statically typed definitions , avoid dynamic lookup. if require flexibility, can use late binding mechanism of interfaces

can explain me "statically typed definitions" , "dynamic lookup" methods , functions in go?

imagine have following code:

type struct {}  func (a a) foo() {     fmt.println("foo called") }  type interface {     foo() } 

i can create variable of type a , call method:

a := a{} a.foo() 

the compiler knows static type of variable, knows method call refers a.foo method. can compile above code use direct call a.foo, fast normal function call.

if instead use variable of type i, things different:

var i = a{} i.foo() 

the variable i can hold type has foo method. in particular case holding a value, won't know @ compile time. instead compiler generates code check dynamic type of i, associated foo method , call method. form of dispatch slower first, has benefit code work type implementing interface.

this similar c++'s distinction between virtual , non-virtual methods, except rather type of dispatch being fixed method @ definition, depends on type of variable use in go.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -