Polymorphism in Go - does it exist? -


I'm trying to make something real simple: goter and setter methods and it seems that the setter methods Not allowed

Looking at this code:

  Package main import "fmt" type MyInterfacer interface {Get () int set (i int)} type MyStruct struct { Data int} func (this mystate) () set up IT (iit) (iit) {this.data = i} func main () {s: = MyStruct {123} fmt.println (s.Get) ) Asset (456) FMT. Printline (S.Gate ()) var milesintheater = s m .Set (789) FMT. The print line (get.)}}   

The set method does not work, because func (this MyStruct) set (i int) , This MyStruct is not an indicator, and changes occur as soon as the function exits, but it will not be compiled by compiling it as this * MyStruct Is there any way?

Here is the correct version of your code (). This is not really polymorphism, but the use of an interface is good go style.

  Package main import "fmt" type MyInterfacer interface {Get () int set (i int)} Type MyStruct Struct {data int} func (get it * MyStruct) (int) .data} func (this * MyStruct) set (i int) {this.data = i} func main () {s: = & amp; MyStruct {123} fmt.Println (s.Get ()) Asset (456) FMT. Printline (s. Gate) var mail micrifier = s m .Set (789) FMT. Printline (Mile (Get.))}    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -