I want to create an interface to abstract from the structures. The structure methods performed the actions and assigned values ​​to their variables, then marshled and sent where necessary. BUT! I read and write that structures with their methods have a link ( func (a *Am)GetL() ), and not a structure object in the method ( func (a Am)GetL() ) of this structure cannot be assigned to an interface. Is it not solvable?

 package main import "encoding/json" func main() { var a Style a = Am{1} Methods(a) as,err := json.Marshal(a) if err==nil{ println(string(as)) } if err!=nil{ println(err.Error()) } } func Methods(in Style) { println("Hellow", in.GetL()) in.SetL(4) println("Hellow", in.GetL()) } type Am struct { L int } func (a *Am)GetL()int{ return aL } func (a *Am)SetL(i int){ aL = i } type Style interface { GetL()int SetL(i int) } 

    1 answer 1

    Pass to the interface a pointer to the structure:

     var a Style a = &Am{1} 
    • That's great) Thank you - Ghost